Closed Yan-Gurevich-MC14 closed 2 weeks ago
This is due to some of your dependencies being CommonJS and tries to import another file/dependency using require
. esbuild
does not transform require
into ES imports, and it does not provide require
for compatibility. Full details in https://github.com/evanw/esbuild/issues/1921
The workaround is to provide this yourself. Ensure your serverless.yml
file contains something like:
build:
esbuild:
banner:
# Compatibility towards CommonJS dependencies that is not rewritten by esbuild.
js: |
import { createRequire } from "node:module";
globalThis.require = createRequire(import.meta.url);
You can also define problematic dependencies as external so they will not be bundled.
Thanks for the clear explanation!
Closing this as it's not an issue with the Serverless Framework itself, but with using CommonJS dependencies in ESM projects. The workaround using createRequire
in serverless.yml
works well in this case. Feel free to reopen if anything else comes up.
Issue description
When i set my project to ESM with "type":"module", lambdas that contain certain packages fail. In my case, it is a lambda that accesses the database via TypeORM. logs in context. When i remove the "type":"module flag it works, but that causes other issues in the project so I ideally would like to continue working with ESM.
The specific issue seems to arise from typeorm using dotenv internally.
Context
serverless_summary.txt
Deployment and invocation: