Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

npm, Yarn and pnpm Compared

Compare npm, Yarn Classic, Yarn Berry and pnpm on disk use, strictness, lockfiles and workspaces, with a command translation table and advice on choosing one.

All three package managers install the same packages from the same registry, so the right choice depends on your team, your project size and your tolerance for new ideas. This lesson compares them fairly: how they store files, which lockfile each writes, how strict they are, and how their commands map onto each other.

At a glance

npm Yarn Classic (1.x) Yarn Berry (2+) pnpm
Ships with Node Yes No No No
Lockfile package-lock.json yarn.lock yarn.lock pnpm-lock.yaml
Install layout Flat node_modules Flat node_modules Plug'n'Play or node_modules Symlinked store
Workspaces Yes Yes Yes Yes
Disk sharing across projects Download cache only Download cache only Cache / zero-install Global store, hard links
Strict about undeclared imports No No Yes (PnP) Yes

Treat the table as a summary of design, not a speed ranking. Speed depends on cache state, network, project size and version, and all three have improved steadily.

Three layouts, one idea

When you install a package, the manager decides where the files live. npm and Yarn Classic create a flat node_modules: nearly every package is hoisted to the top. It is simple but allows a bug called a phantom dependency: your code can import a package you never declared, because some other package happened to pull it in.

pnpm keeps real files in one global store and builds a nested node_modules of links, so a project can only see the packages it declared. Yarn Berry's Plug'n'Play removes node_modules entirely and ships a map file that tells Node where each package lives (often inside zip archives).

Node.js Phantom dependencies, modelled

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The flat result is convenient until web-kit drops logger in a later release; then your app breaks without any change on your side. Strict layouts surface that mistake on day one.

Command translation

Node.js Command translation table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A few details: yarn build works because Yarn treats unknown commands as script names, and pnpm does likewise for scripts that do not clash with built-in commands. yarn dlx and yarn install --immutable are the Yarn Berry spellings; Yarn Classic uses yarn install --frozen-lockfile. Each command gets its own lesson: see Installing Packages and npm Scripts.

How to choose

  • Starting out, or a small tool: npm. It is already installed and every guide assumes it.
  • Many projects on one machine, or a large monorepo: pnpm saves disk space and its strictness catches mistakes early.
  • An existing Yarn codebase: stay on Yarn; migrating gives little for a lot of churn.
  • Teams that want offline, reproducible installs checked into git: look at Yarn Berry's zero-install mode.

Whichever you pick, commit its lockfile and use only one manager per project. Mixing lockfiles is a classic source of "works on my machine" bugs.

If a repository contains both package-lock.json and yarn.lock, someone ran two different managers. Decide on one, delete the other lockfile and add the packageManager field so tools enforce it.

Detecting the manager from lockfiles

Tools often guess the manager from whichever lockfile exists. This stand-in does the same on a list of file names:

Node.js Guess the package manager

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a phantom dependency?

    Flat node_modules layouts let you import undeclared packages by accident; strict layouts prevent that.
  2. 2. Which lockfile belongs to pnpm?

    pnpm writes pnpm-lock.yaml.
  3. 3. Which package manager comes with Node.js?

    npm is bundled with Node.js; the others are separate (Corepack can provide them).
  4. 4. What is the best practice for lockfiles in one project?

    Mixing managers causes inconsistent installs.

Summary

  • npm, Yarn and pnpm share package.json and the registry but differ in layout, lockfile and strictness.
  • npm and Yarn Classic hoist into a flat node_modules; pnpm links from a global store; Yarn Berry can skip node_modules.
  • Strict layouts prevent phantom dependencies.
  • Commands map one to one with small spelling differences.
  • Pick one manager per project and commit its lockfile.

Next: Your First package.json