【发布时间】:2021-05-30 18:25:27
【问题描述】:
我正在尝试使用 Node.js 部署一个非常基本的 Google Cloud 无服务器应用程序,但它一直在 Google Cloud Console 上显示以下错误:
Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /workspace/index.js
require() of ES modules is not supported.
require() of /workspace/index.js from /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /workspace/package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.getUserFunction (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js:29:32)
at Object.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js:77:32)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
Could not load the function, shutting down.
这是我的 index.js 文件:
export function foobar(req, res) {
res.status(200).send("Hello World!");
}
这是我的 package.json 文件:
{
...
"main": "index.js",
"type": "module",
...
}
我正在使用它运行它
gcloud functions deploy foobar --region europe-west1 --runtime nodejs14 --trigger-http --allow-unauthenticated
我已经尝试过以下方法:
-
将 index.js 重命名为 index.mjs 并将 package.json 中的
"main": "index.js"更改为"main": "index.mjs"(错误仍然存在) -
解决方案 1 加上从 package.json 中删除
"type": "module"(错误仍然存在) -
从 package.json 中删除
"type": "module"(引发 SyntaxError: Unexpected token 'export') -
将 index.js 重命名为 index.cjs 并将 package.json 中的
"main": "index.js"更改为"main": "index.cjs"(引发 SyntaxError:意外的令牌“导出”) -
解决方案 4 加上从 package.json 中删除
"type": "module"(引发 SyntaxError: Unexpected token 'export') -
在
gcloud functions deploy中添加--experimental-modules标志(显示无法识别的参数:--experimental-modules) -
解决方案 6,但将
gcloud functions deploy替换为gcloud beta functions deploy(显示无法识别的参数:--experimental-modules) -
完全卸载 Google Cloud SDK 并再次尝试上述所有解决方案
唯一有效的解决方案是解决方案 3 加上使用
exports.foobar = (req, res) => {
res.status(200).send("Hello World!");
}
但是,我想将它用于带有 node-telegram-bot-api module 的 Telegram 机器人,但因为我从 package.json 中删除了 "type": "module",它会引发“SyntaxError: Cannot use import statement outside a导入 Telegram API 时的模块”。
项目结构是使用npm init创建的,如下图。
.
├── index.js
└── package.json
有什么想法吗?我的 Node.js 版本是 v14.17.0,我的 Google Cloud SDK 版本是 v342.0.0。
【问题讨论】:
标签: javascript node.js google-cloud-functions