dev vs devDependencies vs peerDependencies in package.json
Written byPhuoc Nguyen
Created
30 Aug, 2023
Category
JavaScript
When it comes to working with Node.js packages, there are a few different types of dependencies that you'll need to include in your
`package.json`
file. Three of the most common types of dependencies are `dev`
, `devDependencies`
, and `peerDependencies`
. Although these terms may sound alike, each has a unique purpose and use case. Let's take a closer look.#Dev dependencies
`devDependencies`
are dependencies that are only necessary during the development process, and are not required for the final production version of the code. These may include helpful tools like testing frameworks, linters, and bundlers. You can install dev dependencies by running `npm install --dev`
or `npm install --only=dev`
.For example, if you're using Jest as your testing framework, you would include it as a dev dependency in your
`package.json`
file like this:json
"devDependencies": {
"jest": "^29.6.4"
}
When working on a project with multiple developers, it's a good idea to include dev dependencies. This helps ensure that everyone on the team is using the same tools and configurations, which can prevent issues caused by differences between machines. In other words, it keeps everyone on the same page and avoids any unnecessary headaches.
#Dependencies
Dependencies are essential components that must be included in your code for it to run properly. They should be included in the final production version and will be automatically installed when you run
`npm install`
.For instance, if you're using underscore in your code, you would add it as a dependency in your
`package.json`
file like this:json
"dependencies": {
"underscore": "^1.13.6"
}
#Peer dependencies
`peerDependencies`
are dependencies that your package needs to run, but expects the consuming package to provide. Unlike regular dependencies, peer dependencies are not installed automatically when running `npm install`
. Instead, the developer who is using your package must manually install them.For instance, if your package requires a specific version of React to run, you would include it as a peer dependency in your
`package.json`
file like this:json
"peerDependencies": {
"react": "^18.0.2"
}
Using peer dependencies in your package has benefits and drawbacks.
One major benefit is that it can prevent version conflicts between packages. By relying on the consuming package to provide a specific dependency, you ensure that there won't be multiple versions of the same dependency installed. This can avoid issues and make maintenance easier.
Another benefit is that using peer dependencies can make your package smaller and faster to install. Since peer dependencies aren't included in your package's
`node_modules`
folder, it can reduce the size of your package.However, there are some drawbacks to using peer dependencies. One major drawback is that it can add complexity for the developer using your package. If they're not familiar with peer dependencies or don't understand how to manually install them, this could lead to confusion and frustration.
Additionally, if a required peer dependency isn't installed correctly or at all, this could cause errors or unexpected behavior when running your code. This means you may need to provide additional documentation or support for developers who use your package.
#Conclusion
When developing Node.js packages, it's crucial to understand the differences between dependency types. By using the appropriate type for each scenario, you can ensure that your code is well-organized and that other developers who use your package won't run into issues.
#See also
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn