TExcelTAD
Home › JS Packages & Bundlers: npm, Yarn, pnpm, Webpack, Rollup, esbuild › Dependency Types & the node_modules Tree

Dependency Types & the node_modules Tree

package.json has five dependency fields, and they behave differently at install time, at publish time and for your consumers. Then there's the question of where each package lands on disk, which npm answers with hoisting. This lesson covers both.

The five kinds

Field Installed for you Installed for consumers of your package Typical content
dependencies yes yes code your package runs: ms, react-redux
devDependencies yes (unless --omit=dev) no build and test tools: esbuild, typescript, rollup
peerDependencies yes (npm 7+) the consumer must provide it; npm installs it if missing the host your plugin plugs into: react, webpack
optionalDependencies yes, but a failure is not an error yes, same rule platform binaries: @esbuild/linux-x64
bundleDependencies copied into your tarball arrives inside your tarball rare; vendored code

The commands that write them:

bash · try it yourself
mkdir -p kinds && cd kinds
npm init -y > /dev/null
npm install ms@2.1.3 > /dev/null
npm install --save-dev left-pad@1.3.0 > /dev/null
npm install --save-optional is-number@7.0.0 > /dev/null
npm install --save-peer react@19.3.1 > /dev/null
npm pkg get dependencies devDependencies optionalDependencies peerDependencies
ls node_modules
echo "--- production install"
rm -rf node_modules && npm install --omit=dev > /dev/null && ls node_modules
npm warn deprecated left-pad@1.3.0: use String.prototype.padStart()
{
  "dependencies": {
    "ms": "^2.1.3"
  },
  "devDependencies": {
    "left-pad": "^1.3.0"
  },
  "optionalDependencies": {
    "is-number": "^7.0.0"
  },
  "peerDependencies": {
    "react": "^19.3.1"
  }
}
is-number
left-pad
ms
react
--- production install
is-number
ms
react

The first line is the registry talking: left-pad is marked deprecated, and npm shows the author's message on every install (you'll deprecate a version yourself later). react was installed even though it is only a peer: npm 7+ installs missing peers automatically. --omit=dev (what NODE_ENV=production npm install also does) skips left-pad. Deployed servers and Docker images use it to stay small; your bundler, of course, must run before that, because it is a dev dependency.

Note: For an application that ships a bundle, the split barely matters at runtime: the bundle contains whatever it imports, whichever field listed it. It matters for libraries (consumers install your dependencies, never your devDependencies) and for server deployments that run from node_modules.

esbuild and optional dependencies

esbuild's npm package is a small JavaScript wrapper; the real binary lives in one package per platform, all listed as optionalDependencies with os/cpu fields. npm installs only the one that matches the machine:

bash · try it yourself
mkdir -p eb && cd eb
npm init -y > /dev/null && npm install -D esbuild@0.28.2 2>&1 | grep added
npm pkg get optionalDependencies --prefix node_modules/esbuild | head -4
grep -c '"node_modules/@esbuild/' package-lock.json
ls node_modules/@esbuild
npm pkg get os cpu --prefix node_modules/@esbuild/linux-x64
added 2 packages in 2s
{
  "@esbuild/aix-ppc64": "0.28.2",
  "@esbuild/android-arm": "0.28.2",
  "@esbuild/android-arm64": "0.28.2",
26
linux-x64
{
  "os": "linux",
  "cpu": "x64"
}

The lockfile lists all 26 platform packages, so the same lockfile works on a Mac, on Windows and in Linux CI, but only linux-x64 was downloaded here.

Hoisting: where packages land

npm 7+ installs a hoisted (mostly flat) tree: each package goes as high as possible, into the top-level node_modules, unless a different version of the same name is already there. Then it is nested under the package that needs it.

is-odd@3.0.1 depends on is-number@^6.0.0. Install it next to is-number@7 and watch the conflict get nested:

bash · try it yourself
mkdir -p tree && cd tree
npm init -y > /dev/null
npm install is-number@7.0.0 is-odd@3.0.1 > /dev/null
find node_modules -name package.json -not -path '*/.package-lock.json' | sort
npm ls --all
node -e 'console.log(require("is-number/package.json").version)'
node -e 'console.log(require.resolve("is-number", { paths: [require.resolve("is-odd")] }))'
node_modules/is-number/package.json
node_modules/is-odd/node_modules/is-number/package.json
node_modules/is-odd/package.json
tree@1.0.0 /home/learner/tree
├── is-number@7.0.0
└─┬ is-odd@3.0.1
  └── is-number@6.0.0

7.0.0
/home/learner/tree/node_modules/is-odd/node_modules/is-number/index.js

Your code sees is-number 7.0.0 at the top; is-odd's own lookup walks up from node_modules/is-odd/ and finds its nested 6.0.0 first, exactly the resolution walk from earlier.

A hoisting model

js · try it yourself
// Each package asks for name@version. Place it at the top unless the name is already taken by another version.
const requests = [
  { by: 'root', name: 'is-number', version: '7.0.0' },
  { by: 'root', name: 'is-odd', version: '3.0.1' },
  { by: 'is-odd', name: 'is-number', version: '6.0.0' },
  { by: 'root', name: 'chalk', version: '4.1.2' },
  { by: 'chalk', name: 'supports-color', version: '7.2.0' },
  { by: 'chalk', name: 'ansi-styles', version: '4.3.0' },
];

const layout = {}; // path -> version
for (const { by, name, version } of requests) {
  const top = `node_modules/${name}`;
  if (!layout[top] || layout[top] === version) layout[top] = version;
  else layout[`node_modules/${by}/node_modules/${name}`] = version;
}
for (const [path, v] of Object.entries(layout).sort()) console.log(path.padEnd(40), v);
node_modules/ansi-styles                 4.3.0
node_modules/chalk                       4.1.2
node_modules/is-number                   7.0.0
node_modules/is-odd                      3.0.1
node_modules/is-odd/node_modules/is-number 6.0.0
node_modules/supports-color              7.2.0

Notice supports-color and ansi-styles landed at the top even though only chalk asked for them. Your code can now require('supports-color') without declaring it, and it works until chalk stops depending on it. pnpm and Yarn PnP were designed to forbid exactly that.

The order of requests matters in this naive model: whichever version arrives first takes the top slot. Real npm processes the tree breadth-first and prefers the versions your root package.json asks for, then records the result in the lockfile so that order never matters again.

Test yourself

Which dependencies of your library get installed when someone installs your library?

  1. dependencies and devDependencies
  2. dependencies (and optionalDependencies), plus peer dependencies they don't already have
  3. only devDependencies
  4. everything in your lockfile

is-odd needs is-number ^6 while your project needs is-number 7. Where does npm put 6.0.0?

  1. It refuses to install
  2. node_modules/is-odd/node_modules/is-number
  3. node_modules/is-number@6
  4. It upgrades is-odd

Why does the lockfile mention @esbuild/darwin-arm64 even on a Linux machine?

  1. It's a bug
  2. The lockfile describes the tree for every platform; only matching optional packages are downloaded
  3. esbuild needs all binaries
  4. Verdaccio adds it