I use ES6 module syntax on my nodejs server:
package.json
"type": "module"
I (successfully) run my server as local nodejs process. E.g.:
"scripts": {
"dev": "npm outdated ; nodemon --experimental-modules --inspect=4001 main.local.js"
}
Problem: If I start my server via sam local:
"scripts": {
"dev-sam": "sam local start-api --skip-pull-image",
}
I get an error:
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.
My conclusion is: I need to tell the nodejs runtime to enable experimental es6 module support.
Question: How do I do that?
Tried (not working):
"scripts": {
"dev-sam": "sam local start-api --experimental-modules --skip-pull-image",
}
You can't pass arguments to lambda environments. You need to use transpiler. Compile your lambda functions with webpack or similar transpiler.