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.
- This is what the
package.jsonfiles declare. It is a graph, not a tree: Util has two parents. - Both ranges overlap. Any 2.x release at or above 2.3.0 satisfies both.
- 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".
- npm places dependencies as high in
node_modulesas it can, a strategy usually called hoisting. - Because one version of Util satisfies both ranges, a single copy sits at the top level.
- 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/.
- No single version satisfies both
^2.1.0and^3.0.0. - One version stays hoisted; the other is nested under the package that needs it.
- 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.
- A dashed arrow means "I need the host's copy, do not install your own".
- The app provides Host. Recent npm versions install missing peers automatically and report conflicts when ranges cannot be met.
- 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:
| Command | What it tells you |
|---|---|
npm ls util | Every installed copy of Util and the path that led to it |
npm explain util | Why each copy is there, walking back to the root |
npm dedupe | Tries to simplify the tree by moving compatible copies up |
npm query | Selects packages in the tree with CSS-like selectors |
What to take back to your own package
- Use the widest range you have honestly tested for runtime dependencies, so your users' trees can collapse to one copy.
- Do not hold global state that breaks when two copies exist, or document clearly that only one may be loaded.
- Before releasing, install your packed tarball into an example app and run
npm lson your own dependencies. - When a major release of a shared dependency lands, update promptly; lagging behind is what creates Figure 3 in your users' projects.
The semver ranges practice session explains exactly which versions a range admits.