skip to content

With npm, why can `npm ls` show two copies of one library at different majors, and what can `npm explain` and `npm dedupe` do about it?

level: middleimportance: should knowfreq 18%

answer

  1. one top-level slot per name
  2. a rejected range nests a copy
  3. explain accepts a node_modules path
  4. dedupe never installs anything new
  5. find-dupes is the dry run

basics

~20 s

npm's default hoisted layout gives each package name one top-level slot; a dependent whose range rejects that copy gets its own nested copy. npm explain shows which chain demands each copy; npm dedupe merges copies only when one version satisfies every range.

solid answer

~40 s

Under the default `install-strategy=hoisted`, npm places one version of each package in the top-level `node_modules` and nests another copy under any dependent whose range the top-level one does not satisfy, so `chrono-fmt@3` sits at the root and `chrono-fmt@2` under `node_modules/route-map-kit/node_modules`. `npm ls chrono-fmt` shows both; `npm explain node_modules/route-map-kit/node_modules/chrono-fmt` walks back up the chain of ranges that put that copy there. `npm dedupe` (with `npm find-dupes` as its dry run) only collapses copies when one version satisfies every range, and it never installs anything new. Two majors usually cannot merge, so the real fix is upgrading the dependent that still asks for `^2`, or an `overrides` entry if you accept the risk.

code

bash · 12 lines
bash
$ npm ls chrono-fmt
[email protected] /work/trip-planner
├── [email protected]
└─┬ [email protected]
  └── [email protected]

$ npm explain node_modules/route-map-kit/node_modules/chrono-fmt
[email protected]
node_modules/route-map-kit/node_modules/chrono-fmt
  chrono-fmt@"^2.4.0" from [email protected]
  node_modules/route-map-kit
    route-map-kit@"^5.1.0" from the root project

go deeper

for a junior

Recall that npm keeps one copy per name at the top of node_modules and nests another copy only when some dependent cannot use the top one.

for a middle

Walk through npm ls for the paths, npm explain on the nested folder for the chain of ranges, and why npm dedupe only helps when one version satisfies every range.

for a senior

Choose between upgrading the dependent that holds the old major, an overrides entry, or living with the duplicate, weighing bundle size and shared state against breakage risk.

for a principal

Judge when a team should move to another install-strategy, such as linked for library development, against tools and scripts that assume the hoisted layout.

## How the hoisted tree is laid out npm's default `install-strategy` is **`hoisted`**: install each package once in the top-level `node_modules` where possible, and create duplicates deeper in the tree only where necessary. Hoisting works because Node looks for a dependency by walking up the directory tree, so a package at the top is visible to every dependent below it. There is **one slot per package name** at each level, and that rule is where duplicates come from. ## Why a second copy appears Take the trip-planner app: 1. The root declares `chrono-fmt@^3.2.0`, so npm places `[email protected]` at `node_modules/chrono-fmt`. 2. The root also depends on `route-map-kit@^5.1.0`, which declares `chrono-fmt@^2.4.0`. 3. `3.2.1` does not satisfy `^2.4.0`, and the top-level slot is taken, so npm installs `[email protected]` at `node_modules/route-map-kit/node_modules/chrono-fmt`. Both copies are correct: each dependent gets a version inside its own range. The costs are real, though: - **Size.** A bundler that follows both import paths ships both majors of the library to the browser. - **State and identity.** If the library keeps module-level state, or the app compares objects created by one copy against classes from the other, behaviour depends on which copy a file happened to load. - **Patching.** A security fix applied to one major does not reach the other copy. Duplicates also occur between **compatible** versions. npm favours newer versions while installing, even when that causes duplication: if one dependent's `1.0.3` took the top slot first, a later dependent whose range is `1.x` can get `1.9.9` nested beneath it. ## Reading it with `npm ls` and `npm explain` - **`npm ls chrono-fmt`** prints every path to installed copies of that package. Without arguments, `npm ls` shows only the root's direct dependencies; `--all` shows the whole tree. It prints the logical dependency tree, not the physical folder layout. - **`npm explain chrono-fmt`** (alias `npm why`) prints, for each copy, the chain of ranges that required it, ending at "the root project". - **`npm explain node_modules/route-map-kit/node_modules/chrono-fmt`** explains just the copy at that folder, which is exactly what you want when one copy is the surprise. `npm ls` annotates what it finds: | Marker | Meaning | |---|---| | `deduped` | this dependent is served by a copy shown elsewhere in the tree | | `invalid` | the installed version does not satisfy a range that asks for it | | `extraneous` | installed, but nothing in the tree depends on it | | `UNMET DEPENDENCY` | required, but not installed | | `overridden` | the version was set by an `overrides` entry | ## What `npm dedupe` can and cannot do - It **re-evaluates the whole tree** and moves dependencies up where one copy can serve several dependents. Arguments are ignored. - It **never installs new modules**; it only removes copies that an existing higher copy already satisfies. - It **never changes ranges in `package.json`**; raising a direct dependency's range is `npm update --save`, a different operation. - **`npm find-dupes`** runs `npm dedupe` in `--dry-run` mode and only reports. - **`npm install --prefer-dedupe`** (or `prefer-dedupe=true` in `.npmrc`) makes the installer prefer reuse over novelty from the start. For the trip planner, no single version satisfies both `^3.2.0` and `^2.4.0`, so `npm dedupe` leaves both copies. The real fixes are: upgrade `route-map-kit` to a release whose range accepts `chrono-fmt@3`; or force one version with an `overrides` entry in the root `package.json`, accepting that `route-map-kit` runs against a major it never declared; or accept the duplicate. ## The other `install-strategy` values | Value | Layout | |---|---| | `hoisted` (default) | shared copies at the top, duplicates nested only where needed | | `nested` | every package installed in place under its dependent, no hoisting | | `shallow` | only direct dependencies at the top level | | `linked` | packages stored in `node_modules/.store` and linked into place, unhoisted | `linked` only exposes a package's declared dependencies, which is why npm recommends it to package authors for catching imports of packages they never declared.

  • Two copies are compatible versions, `1.0.3` at the top and `1.9.9` nested under a dependent whose range is `1.x`. Why, and what fixes it?
    npm favours newer versions while installing, even when that causes duplication: an earlier dependent's `1.0.3` took the top slot, and the later `1.x` dependent got `1.9.9` nested. `npm dedupe` re-evaluates and deletes the nested copy because the top-level one satisfies both ranges. `npm install --prefer-dedupe` makes the installer prefer that from the start.
  • What does `install-strategy=linked` change about where packages live?
    Instead of hoisting, npm installs packages in `node_modules/.store` and links them into place, so each package only sees the dependencies it declares. npm recommends it to package authors during development: an import of an undeclared package fails locally instead of resolving by accident through a hoisted neighbour. `nested` and `shallow` are the other non-default values.
  • Does `npm dedupe` change any ranges in `package.json`?
    No. It only restructures the installed tree and the lock, never installs new modules, ignores its arguments and works on the whole tree. Raising a direct dependency's range in `package.json` is a job for `npm update --save`, which is a separate decision with its own review.

A hoisted node_modules is like a shared hallway shelf with one slot per title: every room borrows the copy there if its edition suits it, and a room that needs a different edition keeps its own copy. Tidying up can only remove a room's copy when the hallway edition suits that room too.

  • Two copies of one package always means npm's resolver has a bug.
  • npm dedupe can merge any two versions, even across majors.
  • npm dedupe chrono-fmt touches only that one package.
  • npm ls shows the physical node_modules folder layout exactly.
  • npm dedupe may download newer versions so that copies can merge.