dev vs devDependencies vs peerDependencies in package.json
Written byPhuoc Nguyen
Category
JavaScript
Created
30 Aug, 2023
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:"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:"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:"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? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
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