When I wanna build my lambda project by using sam cli, it gives me error like below. I use typescript and ES2022 and wait that it should outputs mjs files. But it outputs cjs files. So, cjs files don't support top-level await. But why outputs mjs files?
Command:
sam build
Error:
Build Failed
Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: ✘ [ERROR] Top-level await is currently not supported with the "cjs" output format
template.yaml (Some part)
......
Metadata:
BuildMethod: esbuild
BuildProperties:
Target: "es2022"
Minify: false
Sourcemap: true
EntryPoints:
- src/handlers/app.ts
tsconfig.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"compilerOptions": {
"lib": ["es2022"],
"module": "nodenext",
"target": "es2022",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false
},
"exclude": ["node_modules"]
}
package.json
{
"name": "my-lambda",
"version": "0.0.1",
"type": "module"
}
You need to explicitly specify the
Formatin sam template.https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html
It's a bit late response. I hope you had already resolved this.