【发布时间】:2021-06-14 23:53:58
【问题描述】:
我希望使用 ES6 自带的原生 import/export。
我在 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。如果有人能指出我出错的正确方向,将不胜感激。
【问题讨论】:
标签: javascript node.js amazon-web-services aws-lambda containers