r/webpack • • Feb 18 '24

Webpack and Node Modules

I'm trying to configure my Serverless Typescript project to use Webpack. Running serverless webpack works without issue, but when I deploy my bundled code to AWS, I run into problems with my node modules. When I try to hit any of my endpoints, I get errors like this :

"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'jwt-decode'

The above example shows jwt-decode being not found, but the same error occurs for all external modules I import. When I look at the code bundles serverless deploys, it is true that they don't contain any node_modules with them. I believe this is because I have externals: [nodeExternals()] set in my webpack.config.js file. I can overwrite this by updating the serverless-webpack plugin configuration in my serverless.yml file like so:

  webpack:
    includeModules: true

But in trying to research this I've found resources (such as this one) saying that you actually don't want to use webpack to bundle related node modules in with your code.

Is it true that I shouldn't be bundling node modules? And, if so, how is my code supposed to reference these external packages when deployed?

Thanks in advance for the help

1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/webdeveric Feb 18 '24

esbuild is a faster transpiler than tsc and it is a bundler too. It doesn't typecheck your code so you'd still need tsc for that. esbuild has plugins that cover most use cases so switching shouldn't be an issue, if you decide to do that.

You can get comparable speed using webpack and swc-loader (https://swc.rs/) if you're not ready to move off of webpack.

You'd typically want to use a Layer for your dependencies if they are shared between many Lambdas. It would reduce the artifact upload size since you're not uploading redundant copies of the node_modules in every zip.

2

u/NovelVeterinarian246 Feb 19 '24

awesome. I ended up configuring esbuild. You're right, way faster and simpler config out of the box. I set up a lambda layer to hold my dependencies and everything is working great. Thanks again for the help!