我试图使用Node.js部署一个非常基本的Google无服务器应用程序,但它一直在Google控制台上显示以下错误:
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我已经尝试过以下几种方法:
"main": "index.js"更改为"main": "index.mjs" (错误持续存在)"type": "module" (错误持续存在)"type": "module" (引发SyntaxError:意外令牌‘导出’)"main": "index.js"更改为"main": "index.cjs" (引发SyntaxError:意外令牌‘导出’)"type": "module" (引发SyntaxError:意外令牌‘导出’)在--experimental-modules)中添加--experimental-modules标志的
gcloud beta functions deploy替换gcloud functions deploy (显示无法识别的参数:唯一有效的解决方案是解决方案3加上使用
exports.foobar = (req, res) => {
res.status(200).send("Hello World!");
}但是,我想将它用于带有node-telegram-bot-api module的电报机器人,但是由于我从package.json中删除了"type": "module",所以在导入Telegram API时,它会引发"SyntaxError:无法使用模块外的导入语句“。
项目结构是使用npm init创建的,如下所示。
.
├── index.js
└── package.json有什么想法吗?我的Node.js版本是v14.17.0,版本是v342.0.0。
发布于 2021-05-31 18:33:55
Google云函数(因此还有Firebase函数)还不支持ESM模块:
https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/233 https://github.com/firebase/firebase-tools/issues/2994
现在,我认为您正在合并使用和打包ES模块。您不必将函数代码打包为ES模块才能使用node-telegram-bot-api模块。只需使用require语法导入它,而不是使用import语法。
发布于 2021-05-30 18:33:45
试着做这样的事情:
const functions = require('firebase-functions');
exports.foobar= functions.https.onRequest((req, res) => {
res.status(200).send("Hello World!");
});发布于 2021-05-30 18:44:58
在Google函数中执行这段代码,以知道哪个版本的Node.js正在运行。
console.log(`Hello Node.js v${process.versions.node}!`);我认为谷歌云需要:节点:'12‘(与Firebase相同)
随着Node版本15.3.0的发布,ES模块可以在没有实验标志的情况下使用。
https://stackoverflow.com/questions/67764536
复制相似问题