Is it possible to build a next.js-style standalone bundle output? #13868
Replies: 3 comments 3 replies
-
In context of docker images for codebases with build step, both |
Beta Was this translation helpful? Give feedback.
-
@GabenGar Thanks for the reply. Since docker supports multi-stage build, it's ok to install both Here is main part of the Dockerfile I used in one of my nextjs project:
As you can see, only two assets folders (
So the I checked the node_modules/ directory, and it's clear that this isn't just a simple copy of some existing node_modules/. It only includes dependencies required for the server runtime. Moreover, upon closer inspection, even these packages appear to be trimmed down—only the JavaScript files that might actually be used in production are kept. You can see that this node_modules/ is just 66.1MB, whereas in my local development environment, node_modules/ takes up a whopping 1.1GB. For Docker images, this makes a massive difference. I'm not very familiar with the Node runtime environment, but I'd guess that constructing such a standalone/ directory would roughly include these:
I think the above points should be sufficient. For me, the hardest part is figuring out how to determine which packages are server-side dependencies. I don't know how Next.js does it - whether they hardcoded a list or performed dynamic analysis during build. This was exactly the point I was trying to make in my original post. If we can solve this issue, then building a standalone version shouldn't be difficult. Finally, when constructing this node_modules/ directory, we should follow symbolic links when copying files. PNPM introduces several build challenges: First is hoisting - some packages only exist in the root node_modules. Another issue is symbolic links - simply copying the apps/webapp/node_modules/ directory won't work since it's full of symlinks. This essentially forces my final Docker image to maintain nearly the same directory structure as the source code to ensure all dependencies resolve correctly. If we could analyze and gather all dependencies into a single location, they would become truly portable. In short, the standalone/ directory lives up to its name—it doesn’t need anything else. Just copy it anywhere, and as long as Node.js is available, it runs directly. No extra mental overhead. I believe that compared to 'being small in size,' 'having no mental or learning burden' is far more important." |
Beta Was this translation helpful? Give feedback.
-
What's missing? Bundle node_modules? Because you really only need the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My project environment: a pnpm monorepo, using RR7 + SSR for webapp, with react-router-hono-server, deployed using Docker.
I found a Dockerfile example in react-router-templates, but it's not for a monorepo. Using a monorepo makes things significantly more complicated. Currently, I have to copy all node_modules/ directories into the Docker image to make node build/server/index.js work, which result in a very large docker image.
I'm not very familiar with the Node runtime environment. I tried moving all UI packages to devDependencies, keeping only the packages that seem to run in the Node environment in dependencies, and installing only dependencies in the Docker image — but no luck. The server still doesn't run and complains about missing modules. Then I tried adding those modules to dependencies, only to have it complain about another missing module. I couldn’t find any documentation clearly explaining what should go in dependencies vs devDependencies.
It would be great if RR could support a Next.js-style standalone output — something that outputs a folder with all runtime assets bundled, so you can copy it anywhere and run it with Node, and it just works.
I'm surprised I couldn't find similar requests in RR's issues or discussions. Am I on the wrong track? Are there other best practices recommended by RR?
Beta Was this translation helpful? Give feedback.
All reactions