DEV Community

Cover image for The difference between dependencies and devDependencies in package.json
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on Originally published at learn.coderslang.com

The difference between dependencies and devDependencies in package.json

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
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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"
  }
}
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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

npm install --save-dev mocha
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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"
  }
}
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

or a shorter version

npm i -D
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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

npm install --only=prod
📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน 📢 แจ้งเตือน: coderslang/the difference between dependencies and devdependencies in packagejson p - ประเด็นร้อน

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.

Top comments (0)