npm / npm/cli

[BUG] npm install in workspace root installs optional peer dependencies of workspace package dependencies

Open
#2,628 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Release 7.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Current Behaviour:

See Steps to Reproduce for details.

When running npm install in the root of a project with workspaces configured, duplicate versions of react are installed because npm installs the version that one of my packages depends on directly AND a version of react that is an optional peer dependency of one of my package's dependencies resulting in:

react@16.14.0 is installed in /root/node_modules/react
react@17.0.0 is installed in /root/apps/next-js-app/node_modules/react

Running the app with npm run dev results in React errors from duplicate versions of react

Expected Behavior:

react@17 satisfies my next app's own dependency AND the peer dependency of @apollo/client and thus only:

react@17.0.0 is installed in /root/node_modules/react

Steps To Reproduce:

I've provided the following reproduction example https://github.com/bradbarrow/npm-peer-deps-workspaces
Run npm install in the root of that repository and note the duplicate versions of react with npm list react

OR follow the full reproduction steps below:

  1. Create a workspace root in /root with a workspace config "workspaces": ["apps/*"]
  2. Create a NextJS app in the apps/next-js-app directory
  3. Add react@17 to the package.json of the NextJS app
  4. Add react-dom@17 to the package.json of the NextJS app
  5. Add @apollo/client@3..3.7 to the package.json of the NextJS app
  6. Note that @apollo/client@3..3.7 has an optional peerDependency of "react": "^16.8.0 || ^17.0.0"
  7. Run npm install in the root of the project
Environment:
  • OS: MacOSX 10.13.6
  • Node: 14.15.4
  • npm: 7.5.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked bradbarrow/npm-peer-deps-workspaces reproduction, run npm install at its workspace root, and inspect the result with npm list react. Trace npm's workspace dependency and optional peer-dependency resolution to determine why duplicate React versions are installed; done means the reproduction installs only react@17.0.0 at the root and avoids the duplicate-version errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.