TExcelTAD

npm vs Yarn vs pnpm

All three install the same packages from the same registry, honour the same package.json and the same semver ranges. They differ in layout, strictness, monorepo features and defaults. This lesson puts them side by side on one project, then gives a command cheat sheet and a way to choose.

One project, three installs

bash · try it yourself
for pm in npm pnpm yarn; do
  mkdir -p $pm && cd $pm
  echo "{ \"name\": \"cmp-$pm\", \"version\": \"1.0.0\", \"dependencies\": { \"chalk\": \"^4.1.2\", \"react-redux\": \"^9.3.0\", \"react\": \"^19.3.1\", \"redux\": \"^5.0.1\" } }" > package.json
  [ $pm = yarn ] && { echo '{ "name": "cmp-yarn", "packageManager": "yarn@4.18.1", "dependencies": { "chalk": "^4.1.2", "react-redux": "^9.3.0", "react": "^19.3.1", "redux": "^5.0.1" } }' > package.json; touch yarn.lock; }
  $pm install > /dev/null 2>&1
  lock=$(ls package-lock.json pnpm-lock.yaml yarn.lock 2>/dev/null)
  top=$(ls node_modules 2>/dev/null | wc -l)
  printf '%-5s lockfile=%-18s top-level node_modules entries=%-3s .pnp.cjs=%s\n' $pm "$lock" "$top" "$([ -f .pnp.cjs ] && echo yes || echo no)"
  cd ..
done
echo "--- can the app require supports-color (not declared)?"
echo "try { require('supports-color'); console.log('yes') } catch { console.log('no') }" > probe.js
for pm in npm pnpm; do cp probe.js $pm/ && (cd $pm && printf '%-5s ' $pm && node probe.js); done
cp probe.js yarn/ && (cd yarn && printf '%-5s ' yarn && yarn node probe.js)
npm   lockfile=package-lock.json  top-level node_modules entries=11  .pnp.cjs=no
pnpm  lockfile=pnpm-lock.yaml     top-level node_modules entries=4   .pnp.cjs=no
yarn  lockfile=yarn.lock          top-level node_modules entries=0   .pnp.cjs=yes
--- can the app require supports-color (not declared)?
npm   yes
pnpm  no
yarn  no

npm exposes every package at the top; pnpm exposes only the four declared ones; Yarn's default PnP has no node_modules at all. Only npm lets the undeclared supports-color through.

Feature comparison

npm 11 Yarn 4 (Berry) pnpm 10
Ships with Node yes via Corepack / install via Corepack / install
Layout hoisted node_modules PnP (default), or node-modules, or pnpm linker symlinked node_modules + content-addressable store
Phantom dependencies allowed blocked (PnP) blocked
Disk use across projects a copy per project zip cache shared (global cache) hard links to one store
Lockfile package-lock.json yarn.lock pnpm-lock.yaml
Frozen install npm ci yarn install --immutable pnpm install --frozen-lockfile
Dependency install scripts run, warn if not in allowScripts off unless enableScripts off unless onlyBuiltDependencies
Workspaces basic, no topological runs workspace:, foreach -t, constraints workspace:, catalog:, -r, --filter
Overrides overrides resolutions overrides (pnpm.overrides)
Patch a dependency (patch-package) yarn patch pnpm patch
One-off command npx yarn dlx pnpm dlx

Command cheat sheet

Task npm Yarn 4 pnpm
install all npm install yarn / yarn install pnpm install
add npm install x yarn add x pnpm add x
add dev npm install -D x yarn add -D x pnpm add -D x
remove npm uninstall x yarn remove x pnpm remove x
run a script npm run build yarn build pnpm build
why is x here npm explain x yarn why x pnpm why x
upgrade within ranges npm update yarn up -R x pnpm update
upgrade to latest npm install x@latest yarn up x pnpm update --latest x
one workspace npm run build -w pkg yarn workspace pkg build pnpm --filter pkg build
all workspaces npm run build --workspaces yarn workspaces foreach -At run build pnpm -r build
publish npm publish yarn npm publish pnpm publish

The same commands, run for real:

bash · try it yourself
mkdir -p y p && echo '{ "name": "y", "packageManager": "yarn@4.18.1", "scripts": { "hello": "echo hello from yarn" } }' > y/package.json && touch y/yarn.lock
echo '{ "name": "p", "version": "1.0.0", "scripts": { "hello": "echo hello from pnpm" } }' > p/package.json
cd y && yarn add ms@2.1.3 > /dev/null && yarn hello && yarn why ms && yarn remove ms | grep YN0085; cd ..
cd p && pnpm add ms@2.1.3 > /dev/null && pnpm -s hello && pnpm why ms | head -1 && pnpm remove ms | grep '^- '
hello from yarn
└─ y@workspace:.
   └─ ms@npm:2.1.3 (via npm:2.1.3)
➤ YN0085: │ - ms@npm:2.1.3
hello from pnpm
ms@2.1.3
- ms 2.1.3

How to choose

  • A new single application: any of them works. npm needs no setup; pnpm's speed and strictness pay off quickly.
  • A monorepo: pnpm (workspaces, catalogs, filters, strictness) or Yarn Berry (constraints, PnP, zero-installs). npm workspaces are fine for two or three packages.
  • An existing project: keep its package manager unless there's a concrete reason to switch. Switching means a new lockfile, which means re-resolving every version: do it in its own pull request, and diff npm ls --all / pnpm list -r --depth Infinity before and after.
  • Whatever you pick: set packageManager, commit the lockfile, delete the other lockfiles, and use the frozen install in CI.

Test yourself

Which package manager lets application code require an undeclared, hoisted package by default?

  1. npm
  2. pnpm
  3. Yarn Berry with PnP
  4. None of them

What is pnpm's equivalent of npm ci?

  1. pnpm ci
  2. pnpm install --frozen-lockfile
  3. pnpm install --immutable
  4. pnpm rebuild

You want to migrate a project from npm to pnpm. What should the pull request contain?

  1. Both lockfiles, to be safe
  2. pnpm-lock.yaml, the packageManager field, the old package-lock.json removed, and a review of the resolved versions
  3. Only a .npmrc change
  4. A new node_modules folder