WittCode💻

npm install for Production

By

Learn how to ignore development dependencies and decrease the size of the node_modules folder when working with npm install in a production environment.

Table of Contents 📖

What is npm-install

The npm install command, or npm i, installs all modules listed inside package.json. However, in a production environment, we don't want to install every package. We want to ignore development packages or packages that are not required for the application to run. To handle this, npm provides two types of packages: dependencies and development dependencies.

Dependencies and Development Dependencies

Dependencies are packages that are required for an application to run. For example, express is a dependency that sets up a web server to handle user requests. Most likely an application using express will need to handle user requests in both development and production. However, we also have development dependencies which are only used in development and testing. Some examples are TypeScript for writing code and nodemon for providing live code updates. DevDependencies are listed in package.json under the key devDependencies.

"devDependencies": {
  "nodemon": "^3.0.1",
  "typescript": "^5.2.2",
}

If we ran npm i with these devDependencies listed inside package.json, they would be saved onto disk, increasing the size of the node_modules folder. When installing dependencies in a production environment, such as on a VPS server, we want to make sure we only install the dependencies that we need. This will prevent the installing of unecessary packages that would just take up disk space.

Installing Production Dependencies

We can ignore development dependencies in multiple different ways.

  • Setting NODE_ENV to production
  • Tagging on --omit=dev to npm i
  • Tagging on --production to npm i

All of these options prevent the installation of development dependencies onto disk. However, it does seem that using --omit=dev is favorable to --production. We can see the benefit of only installing production dependencies by checking the size of the node_modules folder for a project that uses both dependencies and devDependencies.

npm i
du -sh node_modules
557M   node_modules

Note how the size is 557 MB here, this includes both dependencies and devDependencies. Now lets only install production packages.

npm i --omit=dev
du -sh node_modules
448M   node_modules

When installing only production packages, we can see a size different of over 100 MB. We can do the same by toggling the NODE_ENV variable.

NODE_ENV=production npm i
du -sh node_modules
448M   node_modules

NODE_ENV=development npm i
du -sh node_modules
557M   node_modules