The npm Dependency Tree, Figure by Figure

Package authors rarely see the tree their package ends up in, yet that tree decides whether your users get one copy of a shared dependency or three. This tutorial describes a series of figures you can sketch on paper as you read. Each figure has a caption and a short walkthrough. The packages are hypothetical: an app that depends on Package H and Package J, both of which depend on a small utility, Util.

Figure 1: The logical graph

Caption: boxes for App, H, J and Util; arrows from App to H and J; arrows from H and J to Util, each labelled with a range. H's arrow says ^2.1.0, J's says ^2.3.0.

  1. This is what the package.json files declare. It is a graph, not a tree: Util has two parents.
  2. Both ranges overlap. Any 2.x release at or above 2.3.0 satisfies both.
  3. The package manager's job is to turn this graph into folders on disk.

Figure 2: The hoisted install

Caption: a folder diagram. node_modules/ contains h/, j/ and util/ side by side; util/ is tagged "2.4.0".

  1. npm places dependencies as high in node_modules as it can, a strategy usually called hoisting.
  2. Because one version of Util satisfies both ranges, a single copy sits at the top level.
  3. Node.js module resolution walks up parent folders, so both H and J find that same copy.

This is the outcome you want as a library author. It is why caret ranges in libraries are friendlier than exact pins.

Figure 3: The conflict

Caption: same layout, but now J has been changed to require ^3.0.0. The top-level util/ is tagged "2.4.0"; a second util/ tagged "3.0.1" appears inside node_modules/j/node_modules/.

  1. No single version satisfies both ^2.1.0 and ^3.0.0.
  2. One version stays hoisted; the other is nested under the package that needs it.
  3. The app now ships two copies. For pure functions this costs only size. For anything holding state, singletons, or classes checked with instanceof, it causes bugs that are hard to trace.

Figure 4: Where peer dependencies change the picture

Caption: a plugin box, Plugin K, with a dashed arrow to Host labelled "peer: ^5.0.0", and a solid arrow from App to Host.

  1. A dashed arrow means "I need the host's copy, do not install your own".
  2. The app provides Host. Recent npm versions install missing peers automatically and report conflicts when ranges cannot be met.
  3. Use a peer dependency when your package must share the exact instance the app uses, such as a framework or a plugin host.

Figure 5: Inspecting the real tree

Caption: a terminal window showing indented output under the heading "npm ls util".

You do not have to guess. These commands, documented in the npm Docs, show what actually happened:

CommandWhat it tells you
npm ls utilEvery installed copy of Util and the path that led to it
npm explain utilWhy each copy is there, walking back to the root
npm dedupeTries to simplify the tree by moving compatible copies up
npm querySelects packages in the tree with CSS-like selectors

What to take back to your own package

The semver ranges practice session explains exactly which versions a range admits.