TExcelTAD

Strictness & Phantom Dependencies

A phantom dependency is a package your code imports without declaring it. It works by accident: something else you depend on brought it in, and the package manager hoisted it to the top of node_modules, where your code can see it. Then a minor update of that something else drops it, and your build breaks with no change on your side. npm's hoisted layout allows phantoms; pnpm's symlinked layout makes them fail immediately.

The same code, two package managers

chalk@4 depends on supports-color. Our code uses supports-color directly but only declares chalk:

bash · try it yourself
mkdir -p with-npm with-pnpm
cat > app.js <<'JS'
const chalk = require('chalk');
const supportsColor = require('supports-color');   // never declared!
console.log('chalk loaded:', typeof chalk.red, '| stdout colour level:', supportsColor.stdout ? supportsColor.stdout.level : 0);
JS
cp app.js with-npm/ && cp app.js with-pnpm/
echo '{ "name": "app", "version": "1.0.0", "dependencies": { "chalk": "4.1.2" } }' | tee with-npm/package.json > with-pnpm/package.json
echo "== npm"
cd with-npm && npm install > /dev/null && node app.js
ls node_modules
echo "== pnpm"
cd ../with-pnpm && pnpm install > /dev/null && node app.js 2>&1 | grep -E "^Error|code"
ls node_modules
== npm
chalk loaded: function | stdout colour level: 0
ansi-styles
chalk
color-convert
color-name
has-flag
supports-color
== pnpm
Error: Cannot find module 'supports-color'
  code: 'MODULE_NOT_FOUND',
chalk

With npm the phantom import works, because supports-color was hoisted next to chalk. With pnpm, the top level of node_modules contains only what package.json declares, so the import fails right away, on the developer's machine, instead of months later in production.

The fix is always the same: declare what you import.

bash · try it yourself
mkdir -p app && cd app
echo '{ "name": "app", "version": "1.0.0", "dependencies": { "chalk": "4.1.2" } }' > package.json
echo "console.log('supports-color', require('supports-color/package.json').version);" > app.js
pnpm install > /dev/null
node app.js 2>&1 | grep -m1 Error
pnpm add supports-color@^7 2>&1 | grep '^+'
node app.js
Error: Cannot find module 'supports-color/package.json'
+ supports-color 7.2.0 (8.1.1 is available)
supports-color 7.2.0

Phantoms inside your dependencies

Some older packages have phantoms of their own: they require something they forgot to declare, and it only ever worked because npm hoisted it. pnpm offers escape hatches, most specific first:

Setting (in pnpm-workspace.yaml or .npmrc) Effect
packageExtensions add the missing dependency to the broken package's manifest, the clean fix (also report it upstream)
public-hoist-pattern hoist matching packages to the top-level node_modules, for plugins that a tool loads by name (pnpm 9 hoisted *eslint* and *prettier* by default; pnpm 10 hoists nothing unless you ask)
hoist-pattern what may be hoisted into the hidden node_modules/.pnpm/node_modules (default *), visible to dependencies but not to your code
shamefully-hoist=true hoist everything, npm-style; the name says what pnpm thinks of it
node-linker=hoisted a flat npm-like node_modules with no symlinks (for tools that can't follow them, such as some React Native setups)

A packageExtensions entry, for a hypothetical package legacy-lib that forgot to declare ms:

yaml
# pnpm-workspace.yaml
packageExtensions:
  legacy-lib@1:
    dependencies:
      ms: ^2.1.3

Peer dependencies under pnpm

pnpm checks peer dependencies too, but by default it warns rather than failing. Here is the React 18 / react-dom 19 conflict from the peer dependency lesson:

bash · try it yourself
mkdir -p ui && cd ui
echo '{ "name": "ui", "version": "1.0.0" }' > package.json
pnpm add react@18.3.1 react-dom@19.3.1 2>&1 | grep -A3 -i 'unmet\|issues'
echo "--- with strictPeerDependencies: true"
mkdir -p ../ui-strict && cd ../ui-strict
echo '{ "name": "ui-strict", "version": "1.0.0", "dependencies": { "react": "18.3.1", "react-dom": "19.3.1" } }' > package.json
echo 'strictPeerDependencies: true' > pnpm-workspace.yaml
pnpm install > install.log 2>&1; echo "exit code: $?"
grep -E 'ERR_PNPM|unmet' install.log
 WARN  Issues with peer dependencies found
.
└─┬ react-dom 19.3.1
  └── ✕ unmet peer react@^19.3.1: found 18.3.1

dependencies:
+ react 18.3.1 (19.3.1 is available)
--- with strictPeerDependencies: true
exit code: 1
 ERR_PNPM_PEER_DEP_ISSUES  Unmet peer dependencies
  └── ✕ unmet peer react@^19.3.1: found 18.3.1

With strictPeerDependencies: true in pnpm-workspace.yaml (or strict-peer-dependencies=true in .npmrc), pnpm install fails on the same conflict, as npm does by default. In pnpm 10.34 the check runs on pnpm install; a pnpm add of the conflicting pair still only warned when we tried it, so rely on the install step in CI. To accept a specific, known-good mismatch, peerDependencyRules (allowedVersions, ignoreMissing) records the exception in configuration rather than in someone's memory.

A model of the two layouts

js · try it yourself
// Which packages can the root project's code require?
const tree = { chalk: ['ansi-styles', 'supports-color'], 'supports-color': ['has-flag'], 'ansi-styles': ['color-convert'], 'color-convert': ['color-name'] };
const declared = ['chalk'];

function closure(names, seen = new Set()) {
  for (const n of names) if (!seen.has(n)) { seen.add(n); closure(tree[n] ?? [], seen); }
  return seen;
}
const hoisted = [...closure(declared)].sort();       // npm: everything lands at the top
const strict = [...declared].sort();                   // pnpm: only direct dependencies at the top
console.log('npm  top level:', hoisted.join(', '));
console.log('pnpm top level:', strict.join(', '));
console.log('phantoms you could import under npm:', hoisted.filter((n) => !declared.includes(n)).join(', '));
npm  top level: ansi-styles, chalk, color-convert, color-name, has-flag, supports-color
pnpm top level: chalk
phantoms you could import under npm: ansi-styles, color-convert, color-name, has-flag, supports-color

Five packages your code could silently depend on, from declaring just one. In a real application with 50 direct dependencies, npm's top level typically holds several hundred.

Test yourself

What is a phantom dependency?

  1. A dependency that is never used
  2. A package your code imports without declaring it, available only because of hoisting
  3. A dependency with no files
  4. A peer dependency that is optional

Why does require('supports-color') fail in a pnpm project that only declares chalk?

  1. pnpm didn't install supports-color
  2. Only direct dependencies are linked into the top-level node_modules
  3. pnpm blocks CommonJS
  4. supports-color is a peer

Which pnpm setting is the cleanest fix for a third-party package that forgot to declare a dependency?

  1. shamefully-hoist=true
  2. packageExtensions
  3. node-linker=hoisted
  4. strict-peer-dependencies=false