npm / npm/cli

[BUG] peerDependencies not preferred over dependencies when both are present

Open
#7,106 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
Current Behavior

Here is a configuration for the styled-components package.json that I'm testing:
Screenshot 2023-12-27 at 2 45 46 PM

The design goal of this is to provide a dependency version specified by us, but allow it to be overridden by peerDependency in client projects if their installed version is greater than ours. Looking at this RFC that was closed as completed, it seems like this is meant to work in npm. However, both versions are currently installed. Here is an example repo demonstrating the current behavior: https://github.com/quantizor/styled-components-repro

I did an analysis of other package managers like yarn and pnpm, and they both seem to honor peerDependencies as an override over dependencies if both are specified in a library being consumed by a client application.

You can see this in action for the supplied reproduction repository by following these instructions after cloning:

  1. Delete node_modules and package-lock.json
  2. Run yarn install
  3. Observe in node_modules/styled-components that no extra node_modules folder is added
Expected Behavior

If a package declared in both dependencies and peerDependencies, peerDependencies should "win" if a compliant higher version is installed in the client application.

Steps To Reproduce
  1. Clone https://github.com/quantizor/styled-components-repro and cd into the directory
  2. Run npm i
  3. Observe that node_modules/styled-components/node_modules has a duplicated module for postcss (there is a higher version also installed at node_modules/postcss)
Environment
  • npm: 10.2.5
  • Node.js: 18.19.0
  • OS Name: macOS
  • System Model Name: MacBook Air
  • npm config:
; node version = v18.19.0
; npm local prefix = /Users/[redacted]/code/styled-components-repro
; npm version = 10.2.5
; cwd = /Users/[redacted]/code/styled-components-repro
; HOME = /Users/[redacted]

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 package.json configuration and the styled-components-repro repository, then run npm i and inspect node_modules/styled-components/node_modules alongside the top-level node_modules/postcss. Done means a compliant higher peerDependency is used instead of installing a duplicate dependency, matching the behavior described for yarn.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.