Open capndave opened 1 year ago
Looking at this a bit closer, the issue seems to be resolved if I remove type: "module"
from the resulting .esbuild/.build/src/routes/get/package.json
. Is there an option to do this in my serverless.yaml
?
I think I had some trouble with this too. My serverless.yml
contains:
custom:
esbuild:
format: esm
outputFileExtension: .mjs
exclude:
- "@aws-sdk/*"
And I end up with a .mjs
file deployed to Lambda.
I also had to add a banner like https://github.com/evanw/esbuild/issues/1921#issuecomment-1491470829 , but that may have been to deal with a not-fully-ESM module I was using.
@timkingman how did you implemented banner
? thanks
edit: nvm, found it:
custom:
esbuild:
format: esm
outputFileExtension: .mjs
banner:
js: import { createRequire } from 'module';const require = (await import('node:module')).createRequire(import.meta.url);const __filename = (await import('node:url')).fileURLToPath(import.meta.url);const __dirname = (await import('node:path')).dirname(__filename);
My code consists of ES Modules, and I use
"type": "module"
inpackage.json
to make that clear. I do have a few config files (jest, eslint, and prettier) and a script file that are.cjs
extensions. They shouldn't execute when a request is sent to my endpoint, however. Everything works until I tried addingserverless-esbuild
to my project, at which point I get the below error (when I send a request to the endpoint running offline). Am I doing something wrong?Error
serverless.yaml
package.json
src/routes/get/handler.js