#37204·nx

prune-lockfile (npm workspaces) returns a broken or unpruned lockfile and still succeeds

Author: spichenCreated Sep 27, 2026Updated Sep 27, 2026

Current behavior

With npm workspaces, @nx/js:prune-lockfile either writes a lockfile that npm ci rejects, or quietly hands back the root lockfile. The target passes either way.

Three triggers, one app each in the repro below:

  • nested: its [email protected] sits under apps/nested/node_modules because another workspace owns the root slot, and agent-base@6 in the same app needs the root debug@4. The output keeps 4.4.3 and drops 2.6.9. In a bigger repo the same setup throws Following packages could not be mapped to the NPM lockfile instead, and you get the root lockfile back.
  • pinned: pins [email protected], which the root overrides rewrite to 2.1.3. Prune can't find 2.1.2 and falls back. It also copies the app's own overrides, which npm ignores inside a workspace, and drops the root ones npm actually applied.
  • aliased: "ms-alias": "npm:ms" falls back the same way.

Looking at the source, the pruned graph keeps edges from every workspace project, nestMappedPackages never counts a project node as placed, and rehoistNodes only promotes a nested version when the hoisted one is missing from the pruned graph.

I patched those two locally and hit one more: placement ignores shadowing. path-scurry landed under rimraf, next to rimraf/node_modules/[email protected], and resolved that instead of 7.1.3. On our ~5k-entry lockfile, some apps had up to 17 edges like that.

Expected behavior

npm ci accepts the pruned lockfile, and every package resolves the version it does in the workspace. If Nx can't produce that, the target should fail. Returning the root lockfile with exit 0 means the documented npm --omit=dev -f install re-resolves the tree. For us that would drop the overrides we rely on for CVE pins.

Repro

bash
mkdir -p repro/apps/{nested,other,pinned,aliased} && cd repro
echo '{"private":true,"workspaces":["apps/*"],"devDependencies":{"nx":"23.1.1","@nx/js":"23.1.1"},"overrides":{"ms":"2.1.3"}}' > package.json
echo '{"plugins":[]}' > nx.json
app() { echo "{\"name\":\"$1\",\"version\":\"0.0.1\",\"dependencies\":$2$3,\"nx\":{\"targets\":{\"build\":{\"command\":\"mkdir -p apps/$1/dist\",\"outputs\":[\"{projectRoot}/dist\"]},\"prune-lockfile\":{\"dependsOn\":[\"build\"],\"executor\":\"@nx/js:prune-lockfile\",\"options\":{\"buildTarget\":\"build\"}}}}}" > apps/$1/package.json; }
app nested  '{"debug":"2.6.9","agent-base":"6.0.2"}'
app other   '{"debug":"4.3.4"}'
app pinned  '{"ms":"2.1.2"}' ',"overrides":{"debug":"4.3.4"}'
app aliased '{"ms-alias":"npm:ms"}'
npm install --no-audit --no-fund
export NX_DAEMON=false
for a in nested pinned aliased; do
  npx nx run $a:prune-lockfile --skip-nx-cache
  (cd apps/$a/dist && npm ci --dry-run --ignore-scripts)
done

nested passes, then npm ci fails with lock file's [email protected] does not satisfy [email protected]. pinned logs not found in the root lock file: [email protected], and aliased logs ms-alias@npm:ms. Both of those return all 317 root entries.

Environment

nx 23.1.1, @nx/js 23.1.1, npm 11.8.0, Node 24.13.1, macOS arm64.

Probably the same thing as #28832, which was closed as stale.