我想利用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
正在将我的应用程序处理程序作为一个 require 和一个 module 进行调用。然而,我对 /var/runtime/UserFunction.js
没有任何控制权(我不这么认为?)。在我的 Dockerfile
中,我已经指定了 Node14
。我不太清楚我错在了哪里?
我想要做的是运行最新最好的 Node14(例如导入),但没有使用 Babel/转译器“膨胀”我的代码。如果有人能指点我方向,将不胜感激。