47

Does npm have the option to install dependency as peer-dependency like yarn option --yarn, instead of adding it manually for example:

"peerDependencies": {
  "@angular/core": "^7.0.0"
}

Update with more clarification of the question, thanks to @Broncha

The question is how to add a peer dependency to a project. That is

  • npm i dep adds the dependency to the "dependencies" in package.json,
  • npm i -D dep adds the dependency to the "devDependencies" in package.json.

How do I install a dependency that adds it to the "peerDependencies" in package.json?

6
  • Does this answer your question? How to install npm peer dependencies automatically? Commented Apr 9, 2020 at 5:49
  • 1
    @MwamiTovi Unfortunately no, I need to add a peer-dependency to my project, So should I add it manually to peerDependencies? Commented Apr 9, 2020 at 6:03
  • 1
    Yes, as explained in that answer you'll have to handle peer-dependencies manually. Commented Apr 9, 2020 at 10:09
  • 7
    I like how everyone is on the same bandwagon to answer how to install peer-dependencies while the question is how to add a peer dependency to a project. That is, npm i dep, adds the dependency to "dependencies" key in package.json, npm i -D dep adds the dependency to "devDependencies" in package.json. How do I install a dependency that adds it to "peerDependencies" key in package.json? I also searched for this, but I installed it with npm i and moved it to the key manually Commented Jan 24, 2021 at 10:09
  • @Broncha Thanks for describing the question in a better way, I updated the question with your description. Commented Jan 24, 2021 at 12:17

4 Answers 4

29

As for now, there is NO WAY, you can install dependencies as peer-dependencies. You have to install then and manually move them to peerDependencies object in package.json

OLD ANSWER


The automatic install of peer dependencies was removed with npm v3, this feature is aging added in npm v7.

So update your npm to version 7 or higher will solve most of the problems.

If You need to install dependency as a peer dependency.

To install peer dependency, you actually need to manually modify your package.json file.

For example, if you want to install angular's core component library as a peer dependency,

  1. npm i @angular/core

This will add a property in the dependencies object.

"dependencies": {
    "@angular/core": "^7.0.0"
}
  1. Move the installed package name to peerDependencies key.
"peerDependencies": {
    "@angular/core": "^7.0.0"
}

Extra: if you need two versions of the same package then you modify the packge.json file like this,

"peerDependencies": {
   "@angular/core": "^6.0.0"
   "@angular/core": "^7.0.0"
 }
Sign up to request clarification or add additional context in comments.

4 Comments

So this used to exist in npm and now it no longer does? Why?
sure about the “2 versions of same package”? It’s a JSON, twice the same key means a) first is overridden, last wins.. and b) eslint warning. If this is possible they violated the JSON format? ^^ @HashimAziz if it really was possible I think my comment here is the answer
I believe the proper version should be: "@angular/core": ">= 6.0.0 < 8" (if we want to support versions from 6.0.0, but below 8.0.0.)
As indicated below, the --save-peer option works.
28

The --save-peer option of the install command is what you're looking for:

npm install --save-peer <dep>

If the <dep> is already installed under dependencies, then this command will automatically move it under peerDependencies for you.

This is better than manually editing package.json as it will take care of updating the lock file as well.

2 Comments

Best modern answer
this threw errors on nodejs version 22/23, and basically failed, so I'm not sure what info is missing from that answer, but it must be something
8

All the other answers are talking about How NPM command can handle installing the 'peerDeps' of the current 'deps' and 'devDeps' in package.json of current project, installing them automatically.

But the question is ask how to use NPM command with specific flag to install a deps as 'peerDeps' and write into the package.json of current project.

The ANSWER is, unfortunately, there is no such flag even till NPM@7

I guess NPM doesn't treat that a command to install deps, since adding a 'peerDeps' to package.json doesn't really need NPM to install a package to /node_modules/. It is just a file configuration change to package.json. But I understand people don't want to manually add/remove 'deps' in package.json file and want NPM to do that, it may because NPM will handle the order of the 'deps'. Another reason is, 'peerDeps' always use a range of semver, and that has to be edit manually not via a npm install command. like react-redux:

"peerDependencies": {
  "react": "^16.8.3 || ^17"
},

I think NPM@7 should provide a way to support that, since now it is officially able to process the 'peerDeps' and this feature is part of it.

Comments

2

You can use the pkg command:

npm pkg set peerDependencies.@angular/core="^7.0.0"

2 Comments

How is that different than updating package.json manually?
This is handy when you need to edit multiple values, for example, or if you don't want to force the user to manually edit the file. Just copy-paste and press enter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.