node.js - Google Cloud Function 部署错误 : provided function is not a loadable module

标签 node.js typescript google-cloud-functions

我正在尝试从头开始使用 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 field 。当我指定它应该采取 dist使用 --source 明确的文件夹标志,它部署了文件:
gcloud functions deploy helloWorld --source=dist/ --runtime nodejs10 --trigger-http

但是,它仍然不起作用,因为它似乎假设 一切 ,包括 package.json,都包含在这个目录中 - 所以它不会链接任何库依赖项。

最佳答案

原来,.gitignore默认导入 GCF 的 .gcloudignore文件,因为我忽略了我的 .gitignore 中的 js 文件,他们永远不会进入 GCF。
要修复它,只需删除此行:

#!include:.gitignore
我还创建了一个 small template project用于将 Typescript 与 GCF 一起使用。

关于node.js - Google Cloud Function 部署错误 : provided function is not a loadable module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57076892/

相关文章:

Angular 2 电子邮件验证器

node.js - Google Storage 不是构造函数错误

javascript - Firebase 可以在写入数据之前在服务器端转换数据吗?

javascript - 如何通过 Github API 将内容上传到隐藏文件夹名称?

javascript - 是什么导致 javascript 函数不运行它的回调

javascript - <strong>、<cite>、<article> 等的 HTMLElement 类型

javascript - 设置 div 元素的 top 属性

javascript - 如何使用firebase云函数从firebase数据库检索子节点的键值?

javascript - Mongodb/mongoose 只保存一次然后创建 500 错误

javascript - MongoDB 带变量的查找方法