liveBook · Manning

Dependency tree

As explained in Secrets of the JavaScript Ninja, Third Edition

A hierarchical representation of a package's direct and transitive dependencies.

Installing one npm package can download many. A dependency tree represents a project’s direct dependencies and the packages those dependencies require, recursively. For example, installing Express makes npm fetch Express and its dependencies until the tree is complete; the downloaded packages appear in node_modules. section 3.2.1

One request, a full tree

Run npm install express@5. package.json records Express as a top-level dependency, while package-lock.json records the full installed tree. Importing Express in an .mjs file then lets a Node.js application use it. section 3.2.1

The two files serve different purposes. package.json can allow a version range, such as ^5.1.0; package-lock.json specifies exact versions throughout the tree. Commit the lockfile rather than node_modules so npm install can reconstruct the same installed tree on another machine. A lockfile in a package consumed by other projects does not, however, force those projects to use its local dependency tree. section 3.2.3

Do not confuse the installed package tree with an import dependency tree. A module waits until its static imports—and their imports—have loaded before it executes. The first tree concerns installed packages; the second concerns modules needed for execution. section 3.4 section 13.1.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains recursive installation and exact-version reconstruction, without detailing npm’s internal tree-building algorithm.
Examples
Uses an Express installation and its package.json entry to distinguish a requested dependency from the full installed tree.
What it assumes
Introduces npm installation and dependency files while using a Node.js application as the working context.
What you can do after
Operate/configure it by committing package-lock.json instead of node_modules and running npm install to reconstruct the tree.
How it connects
Connects npm dependencies to node_modules, package.json, package-lock.json, version ranges, and static imports.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.