-
Is it possible to bundle React Router's server side code into a single binary and remove the need for node_modules with react-router build? I have a medium sized project with 23 node modules installed in the production dependencies in the package.json file. All of them are used, they're just normal packages that I think many developers would have, eg: firebase, stripe, algoliasearch etc. The resulting docker container is very large at +400Mb 🤯 and the size is mostly due to the node_modules folder which is a whopping +300Mb. I'm using the default out-of-the-box template with the Dockerfile and npm/node22. As a quick test, I tried bundling the build/server/index.js with esbuild and the end result was just 4.7Mb. It didn't work unfortunately but it has made me think that there should be a way to drastically reduce the size of the docker container if I can remove the junk in node_modules - therefore making the cold starts faster, reducing storage and network bandwidth costs. At this time, all ideas are welcome - for example I've heard that Bun can produce a single binary file? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Why do you think you can reduce the size by omitting |
Beta Was this translation helpful? Give feedback.
Yeah you can do that by basically writing your own bundler. Do note however majority of dependencies for server code, aka nodejs code, are either written in CJS or transpiled to it, so that makes them statically non-analyzable. Plus all the weird stuff with native dependencies.
As for Bun, have you looked at the final size of the executable? Nodejs compilation resulted in a 100mb executable on me for just a lib handling CLI with just one command. Granted it depended on
fs
which in turn had to pull an entirety oflibuv
along. I strongly doubt Bun has a bigger wiggle room in this regard.The size of dependencies in general is not a problem for server world, that's why server code is written…