[BUG] Dependencies of packages under a workspace's node_modules are dropped when the root node_modules and lockfile are deleted
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
This is not just a request to bump a dependency for a CVE
- This is not solely a request to bump a dependency for a CVE
Current Behavior
In a workspaces project, if the root node_modules and package-lock.json are deleted but a workspace's own node_modules is left in place, npm install reuses the packages it finds under the workspace without installing their dependencies. Dependencies that were previously hoisted to the root are silently dropped: npm exits 0, prints no warning, and the regenerated package-lock.json omits them too, so committing it carries the gap to every npm ci.
Related but not the same: #9977 (lockfile present) and its fix #9985, which does not change this case. #4471 may be this bug with an unidentified trigger.
Expected Behavior
Every declared dependency is installed and recorded in the new lockfile, as it is when every node_modules folder is deleted first.
Steps To Reproduce
mkdir x && cd x && mkdir -p packages/a
echo '{"name":"r","workspaces":["packages/*"],"dependencies":{"chalk":"5.3.0"}}' > package.json
echo '{"name":"a","version":"1.0.0","dependencies":{"chalk":"4.1.2"}}' > packages/a/package.json
npm install # [email protected] lands in packages/a/node_modules; its deps are hoisted to the root
rm -rf node_modules package-lock.json
npm install # exit 0, no warning
npm ls --all # npm error missing: ansi-styles@^4.1.0, required by [email protected]
# npm error missing: supports-color@^7.1.0, required by [email protected]
grep -c '"node_modules/ansi-styles"' package-lock.json # 0
node -e 'require("./packages/a/node_modules/chalk")' # Error: Cannot find module 'ansi-styles'Control: also deleting packages/a/node_modules before the second install gives a complete tree.
Reproduces every time on npm 12.0.2, 11.19.1 and 10.9.9, with npm_config_userconfig=/dev/null and an empty cache.
With no lockfile, #initTree in build-ideal-tree.js loads the actual tree and then scans it for missing or invalid edges. Tracing that scan on the reproduction shows it visits only the project root, whose edges are a -> null and chalk -> null: with the root node_modules gone, the workspace has no Link yet, so nothing under packages/a/node_modules is examined. Applying #9985's change to 12.0.2 gives the same result, because there is no link for it to follow.
Environment
- npm: 12.0.2 (also 11.19.1, 10.9.9)
- Node.js: v24.19.0
- OS Name: macOS (Darwin 25.6.0)
- System Model Name: MacBook Air
- npm config: defaults (
npm_config_userconfig=/dev/null)
Source: npm/cli