I often see Node.js Lambda projects built using Serverless and including serverless-webpack. It is used to implement Babel and provide the latest JavaScript syntax, especially modules and top-level await.
But according to this blog post, the AWS Lambda environment already supports these features, so we should no longer need to use serverless-webpack.
I face several drawbacks when using webpack with Serverless:
- Code is transpiled (and optionally minified), making it challenging to debug from logs because line numbers and function names may differ from those in your development environment.
- Using ESM-only modules, such as node-fetch and D3, is not straightforward, and the list of ESM-only modules is growing daily.
- There could also be a performance impact because the code is converted to CommonJS. This benchmark test shows that CommonJS could perform 43% worse than ESM.
There are other benefits to Babel and webpack besides transpiling to CommonJS, but these mostly cater to browsers rather than server-side usage.
How do you use ESM with Serverless? Just add "type": "module" to package.json, and it will work in Node.js 14.x and 16.x.
