npm / npm/cli

[BUG] `npm rebuild` does not rebuild optional dependencies if they are in a nested node_modules folder

Open
#6,358 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Priority 2 Release 9.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

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
  1. In this reproduction repository: https://github.com/pkerschbaum/npm-issue-rebuild-not-working-for-optional-deps-in-deeper-nodemodules
  2. Run npm rebuild --foreground-scripts in 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 move package-cfrom optionalDependencies to dependencies (or devDependencies)
  • Also, if you move package-c to the root node_modules folder

So what I observed is:

  • dependencies and devDependencies are rebuilt as expected
  • optionalDependencies are only rebuilt if they reside in the root node_modules folder
  • peerDependencies are rebuilt
    • as expected when peerDependenciesMeta.optional is not set or false
    • have the same issue as optionalDependencies when peerDependenciesMeta.optional is true
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.