I see that nodejs lambda projects are built using serverless and often include serverless-webpack. It is used to implement babel and get the latest javascript syntaxes, especially module and top-level awaits.
But as per this blog post, the AWS lambda environment already supports these features, so we should no longer need to use serverless-webpack.
There are some cons that I face while using webpack in serverless.
- Code is transpiled (and optionally minified), making it challenging to debug from logs, as line numbers and function names may not be the same per your development environment.
- Using ESM only module is not straightforward. Such as node-fetch and d3, and the ESM-only module list is growing daily.
- There could also be a performance impact as the code will be converted to commonJS, and this benchmark test shows that commonsJS could perform 43% worse than ESM.
There are other benefits of babel and webpack besides transpiling to commonJS. But these are mostly catered towards browsers and not serverside usage.
How to use ESM in serverless? Just add "type": "module"
in pacakge.json
in it will work in node 14.x and node 16.x.