The file package.json is an integral part of every Node.js project. It stores all the dependencies that your project needs.

All the dependencies (most often open-source npm modules) are split into two categories: production dependencies and development dependencies.

The difference between the two is that development dependencies won’t be installed in the production environment as they aren’t needed there. It could be something that checks the quality of your code like ESLint, debug tools like Nodemon ot testing helpers like Chai, Mocha, Enzyme.

You can include production dependencies by running the npm install --save command.

For example, here’s how you’d install the module express.

npm install --save express

Once the installation is complete it will appear in the list of dependencies in package.json.

{
  "name": "my_node_project",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.18.11"
  }
}

To install the development dependency, you’ll need to use the flag --save-dev instead of the --save.

npm install --save-dev mocha

This way the module will appear in the section devDependencies in package.json.

{
  "name": "my_node_project",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.18.11"
  },
  "devDependencies": {
    "mocha": "^10.0.0"
  }
}

Once it’s clear how to add the different types of dependencies (dev and prod) to your Node.js project, you should learn how to install them separately or all at once.

To install only dev dependencies from an existing package.json you can use either the full command

npm install --only=dev

or a shorter version

npm i -D

To install only production dependencies, you can use the same flag --only, but now with the argument prod.

npm install --only=prod

Thanks for reading! I hope this tutorial helped you understand the difference between development and production dependencies in package.json and you learned how to use either of them in you Node.js projects.