I am fairly new to Next js and its deployment process. I have recently converted one of my react js projects to Next js in order to advantage of the server-side rendering feature Next js offers. Now comes the deployment time, I figured out the Next build won't deploy without the node_modules
folder present in the server. I use getServerSideProps
in my pages and build with "build": "next build"
command in package.json. The problem is my node_modules folder is close to 300MB (.next
build adds about another 10MB) and I don't think it is the best practice to accompany this much weight with each deployment (I intend to have different instances of this build deployed, therefore 310MB X number of instances) in the server.
Am I doing something wrong here or is this the only way to accomplish this? I appreciate any answers. Thanks...
3 Answers
Update (11/14/22): The previously mentioned "output standalone" feature is no longer experimental:
// next.config.js
module.exports = {
output: 'standalone',
}
Previous answer: The latest Next.js documentation has a good example about how to create a standalone build with an excellent example of how to accomplish it with docker. As of now, the feature is experimental.
// next.config.js
module.exports = {
experimental: {
outputStandalone: true,
},
}

- 357
- 3
- 12
After a bit of research, I am answering my own question. If you use getServerSideProps
you cannot static export your project. Therefore the node_modules folder is required to deploy the project on the server. That doesn't mean you have to FTP the node_modules
, you can do the npm build on the server-side which will download node_modules
to the deployment folder. In my case, the deployment folder weighs around 310MB where the node_modules
folder itself is around 300MB.

- 231
- 1
- 3
- 9
-
4A guy from my company managed to do it without the folder, and we are using server side props. I dont know what sorcery he used. Will let you know if I figure out – Brandon Feb 05 '21 at 15:23
-
1@Brandon Any news? – episage Feb 16 '21 at 11:34
-
1Also interested in the sorcery - Any update @Brandon? – Brendan Sluke Sep 14 '21 at 03:41
-
npm run build on target PC ? i got the error "can't find next " , next need to install manual on customer PC ? @Lance – will Feb 01 '23 at 14:18
-
additionally , i have removed the folder node_modules before cmd of npm run build – will Feb 01 '23 at 14:28
You can use the [@varcel/nnc][1] package to compile the project.
In short:
Install package:
npm i --save-dev @vercel/ncc
Change build command script inside package.json
From: "build": "nest build",
To: "build": "ncc build src/main.ts -o dist",
P.S. In my case entry point for application is src/main.ts
[1]: https://www.npmjs.com/package/@vercel/ncc

- 11
- 1