👨🏫 In this short Blog Post, I will show you the main differences between the multiple ways of declaring dependencies in a package.json
File.
In a `package.json` file for an npm (Node Package Manager) project, you can specify different types of dependencies, each serving a different purpose:
Dependencies:
- Dependencies are the packages that your project directly depends on to run in production.
- These packages are necessary for your application to work as intended when it’s deployed or used by end-users.
- Dependencies specified under the
"dependencies"
key are installed when someone runsnpm install
without the--save-dev
or-D
flag. - These dependencies are typically things like web frameworks, utility libraries, or other modules that your code relies on.
"dependencies": {
"express": "^4.17.1",
"lodash": "^4.17.21"
}
DevDependencies
- DevDependencies are packages that are only needed for development and testing purposes.
- These packages are not required for your application to function in production, so they are not included when your project is deployed.
- DevDependencies specified under the
"devDependencies"
key are installed when someone runsnpm install
with the--save-dev
or-D
flag. - Examples of devDependencies are testing libraries, build tools, code linters, and other development-related utilities.
"devDependencies": {
"jest": "^26.6.3",
"eslint": "^7.20.0"
}
PeerDependencies
- PeerDependencies are a way to specify that your package expects the consumer (the project that installs your package) to provide a specific dependency.
- These dependencies are not installed by your package but are expected to be installed at the same level as your package (i.e., by the consuming project).
- This is useful when your package needs to work with a specific version of another package but doesn’t want to introduce its own copy of that package.
- PeerDependencies are specified under the
"peerDependencies"
key in your package.json.
"peerDependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0"
}
🟰 In summary,
- “dependencies” are essential packages for your project’s runtime.
- “devDependencies” are packages needed during development but not for production
- “peerDependencies” are packages your package relies on but expects the consumer to provide
I hope, it helped you to understand the differences between these 3 dependency types.
Keep Coding 🚀,
Chris