AWS SAM local start-api:设置 lambda nodejs 12.x 标志(例如 --experimental-modules)?

Mar*_*Mun 5 javascript node.js aws-lambda es6-modules aws-sam-cli

我在我的 Nodejs 服务器上使用 ES6 模块语法:

包.json

"type": "module"
Run Code Online (Sandbox Code Playgroud)

我(成功)将我的服务器作为本地 Nodejs 进程运行。例如:

"scripts": {
  "dev": "npm outdated ; nodemon --experimental-modules --inspect=4001 main.local.js"
}
Run Code Online (Sandbox Code Playgroud)

问题:如果我通过 sam local 启动服务器:

"scripts": {
  "dev-sam": "sam local start-api --skip-pull-image",
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Warning: require() of ES modules is not supported. 
require() of /var/task/main.js from /var/runtime/UserFunction.js is an ES module file 
as it is a .js file whose nearest parent package.jsoncontains "type": "module" which 
defines all .js files in that package scope as ES modules.
Instead rename main.js to end in .cjs, change the requiring code to use import(), or 
remove "type": "module" from /var/task/package.json.
Run Code Online (Sandbox Code Playgroud)

我的结论是:我需要告诉 Nodejs 运行时启用实验性 es6 模块支持。

问题:我该怎么做?

尝试过(不起作用):

"scripts": {
  "dev-sam": "sam local start-api --experimental-modules --skip-pull-image",
}
Run Code Online (Sandbox Code Playgroud)

Hal*_*FAK 4

您无法将参数传递给 lambda 环境。您需要使用转译器。使用 webpack 或类似的转译器编译 lambda 函数。