Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Google云函数部署错误:提供的函数不是可加载的模块_Node.js_Typescript_Google Cloud Functions - Fatal编程技术网

Node.js Google云函数部署错误:提供的函数不是可加载的模块

Node.js Google云函数部署错误:提供的函数不是可加载的模块,node.js,typescript,google-cloud-functions,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文件,它看

我正试图用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"
  }
}
我想先设置本地测试环境,所以我使用了。当我运行它时,一切都非常完美:

$ 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.json
main
字段。当我使用
--source
标志明确指定它应该使用
dist
文件夹时,它部署了文件:

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

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

结果是,
.gitignore
默认导入到GCF的
.gcloudignore
文件中,由于我忽略了我的
.gitignore
中的js文件,所以它们永远不会进入GCF

要修复此问题,只需删除此行:

#!include:.gitignore

我还创建了一个用于将Typescript与GCF一起使用的脚本。

添加
/距离
之后!在
.gcloudignore
文件中包括:.gitignore

在我看来,最好不要使用
#!在
.gcloudignore
文件中包括:.gitnore
行,这样当人们向
.gitnore
添加内容时,它也会自动包含在
.gcloudignore

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

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


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

我遇到了这个问题。非常感谢你!同样的问题在这里,真棒的答案!
### .gitignore
node_modules
built

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