AWS SAM本地启动API:设置Lambda Node.js 12.x标志(例如--experimental-modules)?

问题描述 投票:0回答:1

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

package.json

"type": "module"

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

"scripts": {
  "dev": "npm outdated ; nodemon --experimental-modules --inspect=4001 main.local.js"
}

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

"scripts": {
  "dev-sam": "sam local start-api --skip-pull-image",
}

我收到一个错误:

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.

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

问题:我该怎么做?

尝试(不起作用):

"scripts": {
  "dev-sam": "sam local start-api --experimental-modules --skip-pull-image",
}
javascript node.js aws-lambda es6-modules aws-sam-cli
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.