gpt4 book ai didi

javascript - NodeJS 14.x - 原生 AWS Lambda 导入/导出支持

转载 作者:行者123 更新时间:2023-12-05 00:23:58 25 4
gpt4 key购买 nike

我正在寻找使用原生 import/export ES6 自带的。
我在 AWS Lambda 中使用无服务器容器。
我有我的 Dockerfile看起来像这样:

FROM public.ecr.aws/lambda/nodejs:14

COPY app ./

RUN npm install

CMD [ "app.handler" ]
然后我有一个 app包含我的应用程序代码的目录。 app.js代码如下所示:
import { success } from './utils/log';

exports.handler = async () => {
success('lambda invoked');
const response = 'Hello World';
return {
statusCode: 200,
body: JSON.stringify(response),
isBase64Encoded: false,
};
};
从这条线可以看出 import { success } from './utils/log';我正在使用本地进口产品。
在我的 package.json 中,我指定了这个:
  "type": "module"
因为我需要告诉我的应用程序这是一个模块,我想本地导入。如果我不指定这一点,我会得到:
{
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Cannot use import statement outside a module",
"stack": [
"Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",
" at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" 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)",
" at Function.Module._load (internal/modules/cjs/loader.js:769:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)",
" at internal/main/run_main_module.js:17:47"
]
}
所以,我指定它,告诉 Lambda 这是一个模块。但是,对于我的生活,我无法让它工作,我看到了这个错误:
{
"errorType": "Error",
"errorMessage": "Must use import to load ES Module: /var/task/app.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/app.js from /var/runtime/UserFunction.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.\nInstead rename app.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
"code": "ERR_REQUIRE_ESM",
"stack": [
"Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/app.js",
"require() of ES modules is not supported.",
"require() of /var/task/app.js from /var/runtime/UserFunction.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 app.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/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 _tryRequire (/var/runtime/UserFunction.js:75:12)",
" at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1063:30)"
]
}
看起来像 /var/runtime/UserFunction.js正在调用我的应用程序处理程序作为要求和模块。但是,我无法控制 /var/runtime/UserFunction.js (我不相信?)。在我的 Dockerfile我已指定 Node14 .我完全不知道我哪里出错了?
我想要做的是运行最新最好的 Node14(例如导入),而不需要“膨胀”我的代码的 Babel/Transpiler。如果有人能指出我出错的正确方向,我将不胜感激。

最佳答案

如果有人看到这个,遇到同样的问题。请参阅 AWS 官方技术支持提供的以下内容:
“您使用 package.json { "type": "module" } 的指令是正确的,但目前 Lambda Node.js 14 运行时不支持 ECMAScript 模块”。
当我听到更多关于何时提供支持的信息时,我将发布此帖子的更新。我把这个问题留在这里,以防其他人遇到同样的问题。

关于javascript - NodeJS 14.x - 原生 AWS Lambda 导入/导出支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66676555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com