Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Next js production build need node_modules folder in the server?

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...

like image 870
Lance Avatar asked Nov 21 '20 19:11

Lance


People also ask

Do you need node_modules in production?

No, everything is in the bundle after you build. You can take the files defined as output (usually whatever is in the "dist" folder) and stick them on whatever static server you want without the need of the supporting node_modules .

Should I upload node_modules to server?

You should, typically, not upload node modules folder manually. They are the external libraries and are easily available to install separately. So, when moving files through filezilla, move everything but node modules. Then, in your server, simple run npm i before running the application.

Does React build need node modules?

React can be run without node_modules, npm and without installing additional dependencies.

Is it safe to delete node modules folder?

in fact, you can delete any folder with this. like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.


3 Answers

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.

like image 174
Lance Avatar answered Sep 28 '22 18:09

Lance


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,
  },
}
like image 38
hboylan Avatar answered Sep 28 '22 18:09

hboylan


No, you don't need to add your node_modules.

Would recommend you check out the docs about how to go about deploying your Next.js application, but in essence:

  • run next build
  • run next export
  • deploy your out folder (default output folder, can be changed)

Alternatively, you can also use Netlify to deploy and host your application.

like image 21
Sreetam Das Avatar answered Sep 28 '22 19:09

Sreetam Das