Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

A command/param to install a package's peer dependencies or to add them to packages.json #11213

Closed
Meligy opened this issue Jan 20, 2016 · 10 comments

Comments

@Meligy
Copy link

Meligy commented Jan 20, 2016

Let me take angular2 package as example (I know it's not final, but it's good for demonstrating the current issue).

It has the following peer dependencies:

{
  'es6-promise': '^3.0.2',
  'es6-shim': '^0.33.3',
  'reflect-metadata': '0.1.2',
  rxjs: '5.0.0-beta.0',
  'zone.js': '0.5.10' 
}

Arguably at least some of these are rightfully peer dependencies (In the sense that they need to be top-level dependencies to be used by consuming project, in other words, flat dependencies).

Of course when I install it I get the messages about missing peer dependencies. I have read about this in #6565 and that's alright.

Problem

When there are few of these dependencies, it gets a bit silly to copy/paste the name of each one to install them (I have also seen it in a recent comment on #5080).

Proposal

Is it possible to add a flag to npm install command that installs peer dependencies as well?

We would still have the default / implicit experience surprise-free, but also make it easier for those who explicitly want to opt-in and install those peer dependencies.

Another option might be:

  • Add a new npm command that installs peer dependencies, or at least adds them to `packages.

Thoughts?

@kentcdodds
Copy link
Contributor

Seems like a good idea to me. I actually really like the idea of peerDependencies and npm@3 was a nice improvement to how they work. But it would definitely be nice to have a flag to say "install and save the latest of any missing peerDependencies." The problem being, do you save those as devDependencies or dependencies? I think it's probably safe to assume that if you install the package in devDependencies, you could put it's peers into devDependencies also and similarly with dependencies.

How about something like:

npm install angular2 --save --save-peers

And

npm install angular2 --save-dev --save-dev-peers

Thoughts? 💭

@Meligy
Copy link
Author

Meligy commented Jan 20, 2016

Any peer dependency for a package, would be itself a dependency of the consumer of that package, so would be saved in dependencies if saving is asked.

I'd call for putting as little magic into it, and making it explicit. Going with that, I think using devDependencies when asked to --save-dev is absolutely correct.

@othiym23
Copy link
Contributor

Thanks for the discussion! The team spent a while discussing this, and while we agree that something like this would be useful, the CLI team isn't going to have the time to work on it in the next 6-12 months, so I'm going to close this feature request. However, this seems like it might be useful enough that if somebody wanted to put it together as a patch, we'd be happy to land it. We even have some thoughts about what it might look like!

  1. Add a switch, --peers, that can be included as a parameter to npm install. This would install any missing peerDependencies at the top level. It should be extended to work with npm install --save and npm install --save-dev to upgrade package.json in the way you'd expect.
  2. In the case of multiple dependencies needing the same peerDependency, it should be a hard error. This is an unfortunate and unavoidable situation where a human being has to figure out how to get out of dependency hell, because of the many many plugins and tools that require that they be the only version of themselves in the dependency tree (think Flux, old versions of React, etc).

It would also be pretty easy to create a small wrapper script to migrate packages as a one-off, and that wouldn't require any changes to npm at all. In any case, if you want to work on this, please do, and feel free to ask us questions or to open a pull request pretty early in the process if you want more support from the team. Our time is limited, but we have lots of it for the community!

@joshwiens
Copy link

@othiym23 - It is looking like this has some traction on Twitter, would it not make sense to keep the issue open for discussion of the spec for implementation by the community?

@othiym23
Copy link
Contributor

@joshtoo There are over 350 feature requests open on this issue tracker right now, with more being filed all the time. The CLI team is going to have the wherewithal to get to maybe 20-30 in the next year. The CLI team's current policy is to leave open only those feature requests we intend to ship, so that it's clearer to users what the team is actually working on, and is planning to work on soon. We put patch-welcome on closed feature requests where we'd be happy to land community contributions, but have no plans to work on those features ourselves.

@joshwiens
Copy link

@othiym23 - Fair enough, thanks for the clarification.

@jasisk
Copy link

jasisk commented Apr 7, 2016

A couple of ways to do this (someone asked, so I code golfed an answer):

With basic sed:

npm info . peerDependencies | sed -n 's/^{\{0,1\}[[:space:]]*'\''\{0,1\}\([^:'\'']*\)'\''\{0,1\}:[[:space:]]'\''\([^'\'']*\).*$/\1@\2/p' | xargs npm i

... or, if you already have jq and prefer something that doesn't look like this guy typed it:

jq -r '.peerDependencies | to_entries[] | .key + "@" + .value' package.json | xargs npm i

ljharb added a commit to airbnb/javascript that referenced this issue Jul 19, 2016
…amically look up the right version numbers when installing peer deps.

Fixes #956.

This command would be simpler if npm/npm#11213 is ever implemented.
@ljharb
Copy link
Contributor

ljharb commented Jul 19, 2016

That sed command didn't handle quoted package names for me - instead, I wrote this:

PKG=eslint-config-airbnb npm info "$PKG" peerDependencies --json | command sed 's/[\{\},]//g ; s/: /@/g' | xargs npm install --save-dev "$PKG"

which seems to reliably produce and run the command I want. See airbnb/javascript@623dc44 for reference.

@oliverjanik
Copy link

Any progress on this? Can we reopen?

@nathanhleung
Copy link

If anyone comes across this, I wrote a cli program to do this as well:

# If you're using npm
npm install -g install-peerdeps

# If you're using yarn
yarn global add install-peerdeps

cd my-project-directory

install-peerdeps <package>[@<version>]

It works cross-platform (and with both Yarn and NPM) - if you have any problems with it feel free to open an issue on the repo!

jaylaw81 pushed a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017
…amically look up the right version numbers when installing peer deps.

Fixes airbnb#956.

This command would be simpler if npm/npm#11213 is ever implemented.
sensiblegame added a commit to sensiblegame/React-BNB that referenced this issue Oct 23, 2017
…amically look up the right version numbers when installing peer deps.

Fixes #956.

This command would be simpler if npm/npm#11213 is ever implemented.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants