Difference between dependencies, devDependencies and peerDependencies in npm package.json file?

Table of contents

No heading

No headings in the article.

Introduction: Every web development project includes a file called package.json. This file contains project-related metadata. This file instructs npm how to manage the project's dependencies. It also includes other metadata such as the project description, version, and licence. This file is found in the root directory of Node.js project.

package.json.png

To get package.json file

npm init -y

There are 3 types of dependencies in this file

  1. Dependency - It includes all of the packages used in the project, along with their version numbers. So, if you install any library that is necessary in your project and that can be found in the dependencies object.

Syntax to install a dependency in your project

npm i <package_name>
yarn add <package_name>

After installing the module, head to the package.json file and look for the package with its version in the dependencies object.

dependency.png

  1. Dev Dependency - There is an object called dev Dependencies in the package.json file, and it contains all the packages that are utilised in the project during its development phase but not in the production phase, along with their version number.

Syntax to install a dev-dependency in your project

npm i <package_name> --save-dev
yarn add <package_name> --dev

After installing the module, head to the package.json file and look for the package with its version in the dependencies object.

devdependency.png

  1. Peer Dependencies - PeerDependencies are packages that must be installed in the application in order to use your package; they represent compatibility. This means that if someone wants to use your package, they must also install the dependencies. "React" is a very common example of peerdependency.

peerdepedency.png