(aws-lambda-nodejs): `.mjs` file extension and `import.meta` not supported
See original GitHub issueAlthough Lambda now supports Node v14:
.mjs
entry files are rejected by a regex pattern in aws-lambda-nodejsesbuild
is transpiling the scripts tocjs
format (commonjs) but BundlingOptions provides no means to specifyformat: "esm"
, and consequently esbuild polyfillsimport.meta
which breaks all of its uses in the scripts
Reproduction Steps
lib/my-stack.mjs
:
export class MyStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
// ...
new NodejsFunction(this, 'Example', {
runtime: Runtime.NODEJS_14_X,
entry: 'src/entry-file.mjs',
bundling: {
target: 'es2020',
// format: 'esm', <-- should be able to pass this option here
},
};
// ...
}
}
src/entry-file.mjs
:
export async function Example() {
return import.meta.url;
}
What did you expect to happen?
entry-file.mjs
to be allowed to be used as an entry file.import.meta
to be defined andExample()
to return a string.
What actually happened?
Error: Only JavaScript or TypeScript entry files are supported.
Example()
returns undefined sinceimport.meta
is polyfilled with an empty plain object.
Environment
- CDK CLI Version : n/a
- Framework Version:
- Node.js Version: 14.13.0
- OS : n/a
- Language (Version):
Other
-
For the
.mjs
extension: https://github.com/aws/aws-cdk/blob/5d71d8e815529ccde7ffe13bb72f57ae88e896dc/packages/%40aws-cdk/aws-lambda-nodejs/lib/function.ts#L140 -
Allow passing in the
format
option toesbuild
: https://github.com/aws/aws-cdk/blob/5d71d8e815529ccde7ffe13bb72f57ae88e896dc/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L141-L158
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:27 (12 by maintainers)
Top Results From Across the Web
NodeJS 14.x - Native AWS Lambda Import/Export Support
A commonJS file can access ESM files if the have .mjs extension, but as typescript compiles to .js I use some unix commands...
Read more >import.meta - JavaScript - MDN Web Docs
The import.meta meta-property exposes context-specific metadata to a JavaScript module. It contains information about the module, ...
Read more >@aws-cdk/aws-lambda-nodejs - npm
Start using @aws-cdk/aws-lambda-nodejs in your project by running `npm i ... accepts .js, .jsx, .ts, .tsx and .mjs files handler: 'myExportedFunc', ...
Read more >@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
Amazon Lambda Node.js Library. cdk-constructs: Stable. This library provides constructs for Node.js Lambda functions. Node.js Function.
Read more >Using Node.js ES modules and top-level await in AWS Lambda
By setting the type to “module”, you designate all “.js” files in the ... named index.mjs – it will always be treated as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is what I get when using “type”: “module” on package.json. As you can see on the error: require() of ES modules is not supported. require() of /var/task/index.js…
The lambda engine is using require() to load the code on index.js.
https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/
https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/