r/webpack • u/NovelVeterinarian246 • 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: trueBut 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
View all comments
Show parent comment
1
u/webdeveric Feb 18 '24
esbuildis a faster transpiler thantscand it is a bundler too. It doesn't typecheck your code so you'd still needtscfor that.esbuildhas 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
webpackandswc-loader(https://swc.rs/) if you're not ready to move off ofwebpack.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_modulesin everyzip.