【问题标题】:Google Cloud Function deploy error: provided function is not a loadable module谷歌云函数部署错误:提供的函数不是可加载模块
【发布时间】:2019-11-26 08:22:37
【问题描述】:

我正在尝试使用 Typescript 从头开始​​构建我的 GCF 项目。我对这个项目(以及这个问题)的目标是了解云功能的工作原理并手动执行所有设置步骤,这就是我不复制任何示例代码或使用自动化工具的原因。

首先,这是我的src/index.ts 文件:

export const helloWorld = (req: any, res: any) => {
    res.send('Hello, world 4');
}

在我运行 typescript 编译器后,我得到了这个 dist/index.js 文件,这似乎非常合理:

"use strict";
exports.__esModule = true;
exports.helloWorld = function (req, res) {
    res.send('Hello, world 4');
};

我的package.json 设置为指向此文件:

{
  "main": "dist/index.js",
  "scripts": {
    "start": "npx tsc-watch --onSuccess 'npx @google-cloud/functions-framework --target=helloWorld'",
    "deploy": "gcloud functions deploy helloWorld --runtime nodejs10 --trigger-http"
  },
  "dependencies": {
    "@google-cloud/functions-framework": "^1.1.2",
    "tsc-watch": "^2.2.1",
    "typescript": "^3.5.3"
  }
}

我想先设置本地测试环境,所以我使用了functions framework。当我运行它时,一切正常:

$ npx @google-cloud/functions-framework --target=helloWorld
Serving function...
Function: helloWorld
URL: http://localhost:8080/

但是,当我尝试部署时,我收到此错误:

$ gcloud functions deploy helloWorld --runtime nodejs10 --trigger-http
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: Provided code is not a loadable module.
Could not load the function, shutting down.

为什么它说我的index.js 不是可加载模块,我该如何解决这个错误?


更新

原来gcloud 不尊重package.jsonmain 字段。当我指定它应该使用--source 标志明确地使用dist 文件夹时,它部署了文件:

gcloud functions deploy helloWorld --source=dist/ --runtime nodejs10 --trigger-http

但是,它仍然不起作用,因为它似乎假定 所有内容,包括 package.json,都包含在此目录中 - 因此它不会链接任何库依赖项。

【问题讨论】:

    标签: node.js typescript google-cloud-functions


    【解决方案1】:

    事实证明,.gitignore 默认导入到 GCF 的 .gcloudignore 文件中,并且由于我忽略了 .gitignore 中的 js 文件,因此它们永远不会进入 GCF。

    要修复它,只需删除此行:

    #!include:.gitignore
    

    我还创建了一个 small template project 用于将 Typescript 与 GCF 结合使用。

    【讨论】:

    • 我遇到了这个exact问题。非常感谢!
    • 同样的问题,很棒的答案!
    【解决方案2】:

    在您的.gcloudignore 文件中的#!include:.gitignore 之后添加!/dist

    在我看来,最好将#!include:.gitnore 行留在.gcloudignore 文件中,这样当人们向.gitignore 添加内容时,它也会自动包含在.gcloudignore 中。

    对于您希望git 而不是gcloud 忽略的文件和目录,我会在.gcloudignore 文件中显式添加它们。

    让我们举个例子,我有一个 TypeScript 项目。我不希望tsc 的输出受版本控制,但出于显而易见的原因,我确实希望这些文件包含在gcloud 中:built(或dist)文件夹中的文件实际上是我的功能(应用程序)。

    ### .gitignore
    node_modules
    built
    
    ### .gcloudignore file's content
    #!include:.gitignore
    # Do not ignore built (or dist) folder for gcloud:
    !/built
    !/dist
    

    我更喜欢这个解决方案,因为默认情况下,来自 git 的被忽略文件也会被 gcloud 忽略,所以我不必担心人们会忘记我们有多个忽略文件。

    【讨论】:

      【解决方案3】:

      我也遇到了同样的错误,只是在本地。 build 目录原来是丢失的,因为我忘了先创建它。在 IntelliJ 中,我通常将其添加为配置中的“启动前”命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-23
        • 2021-09-06
        • 2021-11-06
        • 2018-04-19
        • 1970-01-01
        • 2021-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多