npm and its different dependencies
What is npm?
npm stands for Node Package Manager. It’s a registry for JavaScript software packages and a place for developers to find, build, and manage code packages. A typical Nodejs, Angular, React project has multiple packages and those are defined in the package.json file. These packages can be differentiated as `dependencies`, `devDependencies`, `peerDependencies` and `optionalDependencies`. Check the example code below:
{ "name": "sample-project", "dependencies": { "aPackage": "^1.0.0" }, "devDependencies": { "bPackage": "~2.0.0" }, "peerDependencies": { "cPackage": "^2.2.2" }, "optionalDependencies": { "dPackage": "~3.0.0" } }
Let us get an understanding of what is the use of each object defined in package.json
dependencies
Object
The dependencies object specifies the packages that you need to run your code. For example Angular, React, Firebase, etc. When you run npm install aPackage
, npm installs the package and adds it to the dependencies object in the package.json file. If you are working on someone else’s code (let’s say you cloned a repository from GitHub), and you run npm install from the root folder of the project, npm will install all the dependencies that are listed in the dependencies object.
devDependencies
Object
The devDependencies object maps the packages that you will only need during the development of your project and not in the production. For example, a testing framework like Jest,, or auto restart server like nodemon. When you run `npm install bPackage –save-dev` , npm installs the package and adds it to the devDependencies object in the package.json file. If you run npm install on a cloned repository, npm assumes that you are developing the project. That’s why it will also install all the dependencies listed in the devDependencies object. If you do not want to install devDependencies you can use the `–omit=dev` flag, like so: `npm install –omit=dev`
`peerDependencies` Object
The peerDependencies object is a little different to the other dependencies. Packages use them when they require a specific version of another package to function correctly but don’t want to install it directly.. Instead, it expects the consumer of the package to install the required dependency. For example, let’s say you’re developing a React component library that relies on a specific version of React Router for routing functionality. You would specify React Router as a peer dependency in your package.json like this:
{ "name": "my-component-library", "version": "1.0.0", "peerDependencies": { "react-router-dom": "^5.0.0" } }
When installing your component library using npm, users must ensure that “react-router-dom” is installed in their project. npm does not automatically install it alongside your library. This allows consumers to have more control over their dependency tree and ensures compatibility with their specific project requirements.
By using peer dependencies, package authors can avoid dependency conflicts and ensure that their package functions correctly within different project setups. If you do not want to install peerDependencies you can use the `–legacy-peer-deps` flag, like so: `npm install –legacy-peer-deps`
`optionalDependencies` Object
The optionalDependencies object allows you to specify dependencies that are not essential for the functioning of your package but can enhance its capabilities if present. Unlike regular dependencies, they won’t prevent installation if they fail to download or build. Optional dependencies are commonly used for packages that have additional features or optimizations but can still function without them. They’re particularly useful for packages that provide support for multiple environments or platforms, where certain dependencies may not be universally available.
Conclusion
Understanding the distinctions between npm’s dependencies is crucial for efficient package management. By utilizing regular dependencies, devDependencies, peerDependencies, and optionalDependencies appropriately, developers can streamline their projects and enhance productivity. Stay informed and leverage the full potential of npm’s dependency ecosystem for smoother development workflows.