Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add npm dependency as peer dependency

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?

like image 953
Amr Salama Avatar asked Apr 09 '20 05:04

Amr Salama


2 Answers

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.

like image 123
Wayne Mao Avatar answered Oct 17 '22 08:10

Wayne Mao


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"
 }
like image 37
Rohit Nishad Avatar answered Oct 17 '22 09:10

Rohit Nishad