[BUG] npm dedupe
fails to dedupe newer version of dependency when older version of dependency is at root level in package-lock.json
#5307
Labels
Bug
thing that needs fixing
Needs Triage
needs review for next steps
Release 8.x
work is associated with a specific npm 8 release
Is there an existing issue for this?
This issue exists in the latest npm version
Current Behavior
When running
npm dedupe
on a package tree containing several instances of a package dependency at the same semver range, the package is not lifted to the root level of the package tree, and multiple versions of the package are installed.I believe this is a result of npm installing an older version of the package at the root-level, preventing the newer shared version from being lifted to the root level. This hypothesis is supported by the fact that removing the package that relies on the older version of the dependency results in
npm dedupe
working correctly.In the linked repo,
@hashicorp/react-combobox
depends on@reach/combobox
, which depends on@reach/[email protected]
. Since this package was the first installed, npm stored@reach/[email protected]
atnode_modules/@reach/auto-id
, and persisted this topackage-lock.json
. When@hashicorp/react-text-input
and@hashicorp/react-textarea-input
were installed, npm was unable to dedupe@reach/[email protected]
because the only available storage location was already taken by@reach/[email protected]
.I'm fairly sure this is a result of npm using layout data from
package-lock.json
since installing without a lockfile results in correct deduplication. This leads me to believe that modern versions of npm have a dedupe algorithm that can solve for this specific case, but it instead relies on the layout information frompackage-lock.json
, resulting in a package tree that's different from what you'd receive if you had runnpm install
without a lockfile.Expected Behavior
Packages that depend on the same semver range of a specific package have their dependencies deduped even if the
package-lock.json
lockfile has a different version of the package at the root level.In the provided repro, after
npm dedupe
I'd expect@reach/[email protected]
to be moved intonode_modules/@reach/combobox/node_modules/@reach/auto-id
, and@reach/[email protected]
to be moved fromnode_modules/@hashicorp/react-text-input/node_modules/@reach/auto-id
tonode_modules/@reach/auto-id
.Steps To Reproduce
npm install
followed bynpm dedupe
.@reach/auto-id
atv0.17.0
is not deduped by runningnpm ls @reach/auto-id
.Environment
The text was updated successfully, but these errors were encountered: