Understanding the Difference between Dependencies, devDependencies, and peerDependencies

Hi, this is Charu from Classmethod. In this blog, we will walk through the differences between Dependencies, devDependencies, and peerDependencies.

In every web application project, we have a file called package.json. This file contains all the relevant data regarding the project. It contains all the dependencies used and all the version numbers that will be used in the project. Dependencies are external libraries, packages, or modules that your project relies on to function properly. These are categorised into three main types: dependencies, devDependencies, and peerDependencies.

Let's get to know more about each of them.

Dependencies:

In package.json file, there is an object called dependencies and it consists of all the packages that are used in the project with its version number. So, whenever you install any library that is required in your project, that library you can find in the dependencies object. When you install a package as a dependency, it gets listed in the dependencies section of your package.json file.

These are the essential external packages that your application needs to run successfully. These packages are usually required for your application's core functionality. They include libraries and modules that your application directly uses to provide its intended features.

To install a package as a dependency, you would use the following command:

npm install package-name

Example: Installing the aws-cdk-lib module using the following command:

npm install aws-cdk-lib

After the module is installed, if you navigate to the package.json file, you can find the aws-cdk-lib with it's version in the dependencies object as shown below:

Dev Dependencies:

In package.json file, there is an object called as dev Dependencies and it consists of all the packages that are used in the project in its development phase and not in the production or testing environment with its version number. So, whenever you want to install any library that is required only in your development phase then you can find it in the dev Dependencies object.

These packages include tools, testing frameworks, build systems, and any other utilities that help you develop and maintain your application. devDependencies do not get included when your application is deployed to production since they are not needed for its core functionality.

Use the below command to add more dev Dependencies in your project:

npm install package-name --save-dev

You can find all your devDependencies under devDependencies object of your package.json file as shown below:

Peer Dependencies:

This concept is most relevant in situations where multiple packages depend on the same third-party library but might require different versions of it. peerDependencies allow you to specify which versions of a package your module is compatible with.

Note: These dependencies are not automatically installed. You need to change the package.json file manually. To specify a peerDependency in your package.json, you would do something like this:

"peerDependencies": {
  "package-name": "^version-number"
}

Here, the ^ symbol indicates that your program is compatible with any version of the module within the specified version range.

Conclusion:

In this blog, we learned the distinction between dependencies, devDependencies, and peerDependencies. A dependency is a library that a project needs to function effectively. DevDependencies are the packages a developer needs during development. A peer dependency specifies that our package is compatible with a particular version of an npm package.

Thank you for reading until the end. Hope you learned something new from this blog!

Happy learning:)