npm / npm/cli

[BUG] Optional peer dependency influences node_modules layout

Open
#8,028 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Priority 2
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

Given the following projects:
Project A

{                                                                                                                                                                                   
  "name": "projecta",                                                                                                                                                               
  "version": "1.0.0",                                                                                                                                                               
  "main": "index.js",                                                                                                                                                               
  "dependencies": {                                                                                                                                                                 
    "prettier-plugin-organize-imports": "4.1.0"                                                                                                                                     
  }                                                                                                                                                                                 
} 

Project B

{                                                                                                                                                                                   
  "name": "projectb",                                                                                                                                                               
  "version": "1.0.0",                                                                                                                                                               
  "main": "index.js",                                                                                                                                                               
  "dependencies": {                                                                                                                                                                 
    "projecta": "1.0.0",
   "typescript: "4.9.5"                                                                                                                                     
  }                                                                                                                                                                                 
}

When installing packages in Project B prettier-plugin-organize-imports is located under node_modules/projecta/node_modules because prettier-plugin-organize-imports contains

"peerDependencies": {
    "@vue/language-plugin-pug": "^2.0.24",
    "prettier": ">=2.0",
    "typescript": ">=2.9",
    "vue-tsc": "^2.0.24"
  },
  "peerDependenciesMeta": {
    "@vue/language-plugin-pug": {
      "optional": true
    },
    "vue-tsc": {
      "optional": true
    }
  },

and vue-tsc has a peer-dependency on typescript 5.0.0

"peerDependencies": {
		"typescript": ">=5.0.0"
	},

When project B is moved to typescript 5.0.0 then prettier-plugin-organize-imports is located under node_modules/ directly.
To reproduce:

  • npm pack in projectA
  • npm install ../projectA/projecta-1.0.0.tgz

After changing the typescript Version in project B to 5.6.2 the layout of node_modules changes
One workaround is to override the vue-tsc dependency of prettier-plugin-organize-imports to vue-tsc in version 2.0.20 which has a peer dependency to typescript "*".

Expected Behavior

The optional peer-dependency should not influence the layout of node_modules if not present. So the layout of node_modules should be the same in both cases (typescript 4.9.5 and typescript 5.0.0).

Steps To Reproduce
  1. npm pack in projectA
  2. npm install ../projectA/projecta-1.0.0.tgz
  3. observe node modules layout
  4. update typescript dependency in project b
  5. after a clean install in project b the node_modules structure changes
Environment
  • npm: 10.8.1 (also in 9.8.1 and 10.9.0)
  • Node.js: tried 18 /20 /22
  • OS Name: Ubuntu 20.04
  • System Model Name: Lenovo Thinkpad
  • npm config:
; node bin location = /home/XXX/.nvm/versions/node/v22.11.0/bin/node
; node version = v22.11.0
; npm local prefix = /home/XXX/work/npm_repro/projectB
; npm version = 10.9.0
; cwd = /home/XXX/work/npm_repro/projectB
; HOME = /home/XXX
; Run `npm config ls -l` to show all defaults.

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 Project A and Project B package.json files and reproduce the layout using npm pack and npm install ../projectA/projecta-1.0.0.tgz. Compare clean installs with TypeScript 4.9.5 and 5.0.0, focusing on how the optional peer dependencies affect placement. Done means the optional peer dependency no longer changes the node_modules layout when it is absent.

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.