16

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?

Amr Salama
  • 561
  • 1
  • 5
  • 18
  • Does this answer your question? [How to install npm peer dependencies automatically?](https://stackoverflow.com/questions/35207380/how-to-install-npm-peer-dependencies-automatically) – MwamiTovi Apr 09 '20 at 05:49
  • 1
    @MwamiTovi Unfortunately no, I need to add a peer-dependency to my project, So should I add it manually to peerDependencies? – Amr Salama Apr 09 '20 at 06:03
  • 1
    Yes, as explained in that answer you'll have to handle `peer-dependencies` manually. – MwamiTovi Apr 09 '20 at 10:09
  • 4
    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 – Broncha Jan 24 '21 at 10:09
  • @Broncha Thanks for describing the question in a better way, I updated the question with your description. – Amr Salama Jan 24 '21 at 12:17
  • @AmrSalama I noticed that you update the question and my answer does not fulfill the context of the updated question. that why i update my answer i know it's kind of "now answer" but this is what it is!. – Rohit Nishad Aug 26 '21 at 09:32

3 Answers3

14

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

- I noticed that you update the question and my answer does not fulfill the context of the updated question.

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"
 }
Rohit Nishad
  • 1,864
  • 2
  • 15
  • 29
5

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.

Wayne Mao
  • 171
  • 2
  • 7
0

you can not do it directly in npm 3 so check below reference for detials https://stackoverflow.com/a/35207983/10309265 You can do it by either way reference: https://stackoverflow.com/a/35690137/10309265

Shohanur
  • 116
  • 7