What's the difference between dependencies, devDependencies, and peerDependencies in NPM package.json file?

Better Stack Team
Updated on March 11, 2024

When you create a new Node.js project, you’ll notice a package.json file in the root directory. This file contains metadata about your project, including the dependencies required to run it. There are three types of dependencies that can be specified in the package.json file: dependenciesdevDependencies, and peerDependencies. Here’s what each of them means:

  • dependencies: These are packages that your application needs to run in production. When you run npm install in your project directory, these packages will be installed automatically. For example, if your application uses the express package, you would list it as a dependency in your package.json file.
  • devDependencies: These are packages that are only needed during development. They are not required to run your application in production. When you run npm install with the -dev flag, these packages will be installed. For example, if you use mocha for testing, you would list it as a devDependency in your package.json file.
  • peerDependencies: These are packages that your package relies on but expects the consumer to provide. They are not installed automatically when someone installs your package. Instead, when someone installs your package, they will get a warning if the specified packages are not found in their node modules. For example, if your package relies on react, you would list it as a peerDependency in your package.json file.

Here’s an example package.json file that includes all three types of dependencies:

 
{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "mocha": "^9.1.3"
  },
  "peerDependencies": {
    "react": "^17.0.2"
  }
}

In this example, express is a dependencymocha is a devDependency, and react is a peerDependency.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github