[BUG] `npm rebuild` does not rebuild optional dependencies if they are in a nested node_modules folder
Nobody has claimed this yet.
- 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
npm rebuild --foreground-scripts does not execute install script of package-c ❌
Expected Behavior
npm rebuild --foreground-scripts executes install script of package-c (and therefore prints installing package-c!)
Steps To Reproduce
- In this reproduction repository: https://github.com/pkerschbaum/npm-issue-rebuild-not-working-for-optional-deps-in-deeper-nodemodules
- Run
npm rebuild --foreground-scriptsin the root directory of this repo.
--> package-c not installed
Notes:
The dependency chain is
node-starter
--> dependency: package-a
--> dependency: package-b
--> optionalDependency: package-c
And the packages are nested like this (which is a totally valid node_modules structure, and something similar to this happens with pnpm!):
.
├── package.json
└── node_modules/
└── package-a/
├── package.json
└── node_modules/
├── package-b/
│ └── package.json
└── node_modules/
└── package-c/
└── package.json
But I found two ways to get a rebuild for package-c:
- In package.json of
package-b, if you movepackage-cfromoptionalDependenciestodependencies(ordevDependencies) - Also, if you move
package-cto the root node_modules folder
So what I observed is:
dependenciesanddevDependenciesare rebuilt as expectedoptionalDependenciesare only rebuilt if they reside in the root node_modules folderpeerDependenciesare rebuilt- as expected when
peerDependenciesMeta.optionalis not set or false - have the same issue as
optionalDependencieswhenpeerDependenciesMeta.optionalis true
- as expected when
Environment
- npm: 9.6.4 (but also an issue with 8.19.3)
- Node.js: 16.19.0
- OS Name: Ubuntu 22.04.1 LTS (WSL 2)
- System Model Name: Asus ROG Zephyrus M16 (2022)
- npm config:
; node bin location = /home/pkerschbaum/.nvm/versions/node/v16.19.0/bin/node
; node version = v16.19.0
; npm local prefix = /home/pkerschbaum/workspace/npm-issue-rebuild-not-working-for-optional-deps
; npm version = 9.6.4
; cwd = /home/pkerschbaum/workspace/npm-issue-rebuild-not-working-for-optional-deps
; HOME = /home/pkerschbaum
; Run `npm config ls -l` to show all defaults.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running npm rebuild --foreground-scripts in the linked reproduction repository and inspect how npm traverses nested node_modules entries during rebuild. Trace the handling of optional dependencies; the fix is complete when package-c's install script runs and prints installing package-c! for the reported nested layout.
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