Skip to content

@nx/js:prune-lockfile throws "Cannot read properties of undefined (reading 'name')" when transitive dependencies have multiple versions #34322

@jrohland

Description

@jrohland

Current Behavior

When running @nx/js:prune-lockfile on a monorepo with workspace packages, the executor throws:

NX   An error occured while creating pruned lockfile

Original error: Cannot read properties of undefined (reading 'name')

TypeError: Cannot read properties of undefined (reading 'name')
    at switchNodeToHoisted (node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:165:31)
    at node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:159:13
    at Map.forEach (<anonymous>)
    at rehoistNodes (node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:145:23)

The build still succeeds (falls back to the root lockfile), but the error is logged.

Expected Behavior

The lockfile should be pruned without errors.

GitHub Repo

https://github.com/jrohland/nx-prune-lockfile-bug-repro

Steps to Reproduce

git clone https://github.com/jrohland/nx-prune-lockfile-bug-repro
cd nx-prune-lockfile-bug-repro
yarn install
yarn nx run my-app:prune-lockfile

Nx Report

Node           : 24.9.0
OS             : linux-x64
Native Target  : x86_64-linux
yarn           : 4.12.0
daemon         : Available

nx             : 22.4.5
@nx/js         : 22.4.5
@nx/workspace  : 22.4.5
@nx/devkit     : 22.4.5
@nx/esbuild    : 22.4.5
typescript     : 5.6.3

Failure Logs

NX   An error occured while creating pruned lockfile

Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
To prevent the build from breaking we are returning the root lock file.

Original error: Cannot read properties of undefined (reading 'name')

TypeError: Cannot read properties of undefined (reading 'name')
    at switchNodeToHoisted (/tmp/nx-minimal-repro/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:165:31)
    at /tmp/nx-minimal-repro/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:159:13
    at Map.forEach (<anonymous>)
    at rehoistNodes (/tmp/nx-minimal-repro/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:145:23)
    at pruneProjectGraph (/tmp/nx-minimal-repro/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:28:5)
    at createLockFile (/tmp/nx-minimal-repro/node_modules/nx/src/plugins/js/lock-file/lock-file.js:204:79)

Package Manager Version

Yarn 4.12.0

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

The bug is in rehoistNodes() in project-graph-pruning.js. When pathLengthToIncoming() returns undefined for all nested nodes (because none have a path to a direct dependency in package.json), closest remains undefined and is passed to switchNodeToHoisted().

This happens with transitive dependencies that have multiple versions where neither version is reachable from a direct dependency.

Suggested fix:

if (closest) {
    switchNodeToHoisted(closest, builder, invBuilder);
}

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions