See https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/
Here is my template file:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: nodejs18.x
Resources:
Lambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: app.handler
Timeout: 10
Architectures:
- arm64
Events:
Api:
Type: HttpApi
Properties:
Path: /events
Method: POST
When running the plugin, the final template file generated is this:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: nodejs18.x
Resources:
Lambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: Lambda
Handler: app.mjs.handler
Timeout: 10
Architectures:
- arm64
Events:
Api:
Type: HttpApi
Properties:
Path: /events
Method: POST
Note that app.mjs.handler is wrong. It should be just app.handler.
In my webpack.config.js, I have:
output.library.type set to "module".
output.library.chunkFormat set to "module".
experiments.outputModule set to true.
In my package.json, type is set to "module".
With this, .mjs files are generated instead of .js files. But because the file generated is called app.mjs.js instead of app.mjs, Lambda doesn't load it as an ES Module.
See https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/
Here is my template file:
When running the plugin, the final template file generated is this:
Note that
app.mjs.handleris wrong. It should be justapp.handler.In my
webpack.config.js, I have:output.library.typeset to"module".output.library.chunkFormatset to"module".experiments.outputModuleset totrue.In my
package.json,typeis set to"module".With this,
.mjsfiles are generated instead of.jsfiles. But because the file generated is calledapp.mjs.jsinstead ofapp.mjs, Lambda doesn't load it as an ES Module.