Skip to content
Shubham JangleResources
Node.js

Explore topics

npm vs Yarn vs pnpm vs Bun

npm vs Yarn vs pnpm vs Bun. What's the difference?

  • All four install your project's dependencies from package.json.
  • All four record exact resolved versions in a lock file, so every machine and CI run installs the same dependency tree.
  • The main difference is how they store and lay out packages on disk, plus speed and extra features.

npm (Node Package Manager)

  • npm comes bundled with Node.js by default, so it works without any extra setup.
  • It installs dependencies into each project's node_modules folder using a mostly flat (hoisted) structure.
  • It locks versions in package-lock.json.
  • Because of the flat structure, your code can import a package you never declared in package.json, as long as another dependency installed it. This is called a phantom dependency.
    • It works until that dependency changes or removes it, and then your app breaks unexpectedly.
  • npm keeps a download cache, but each project still gets its own extracted copy. With 10 React projects, you could end up with 10 separate copies of React on disk.

Yarn

  • Facebook released Yarn in 2016, when npm installs were slow and could produce different dependency trees on different machines.
  • Yarn introduced a lock file by default, yarn.lock, plus parallel downloads and an offline cache. npm later added its own package-lock.json (npm 5, 2017).
  • Yarn Classic (v1) still uses node_modules and is in maintenance mode. Modern Yarn (v2+, "Berry") is the actively developed version.
  • Modern Yarn can use Plug'n'Play (PnP):
    • There is no node_modules folder.
    • Yarn generates a .pnp.cjs file that tells Node.js exactly where each package lives in Yarn's cache.
    • Packages are stored as zip archives, so installs are fast and undeclared imports fail with a clear error.
    • Some tools expect a real node_modules. For those, Yarn can switch back with nodeLinker: node-modules.

pnpm (Performant npm)

  • pnpm stores each package version only once in a global content-addressable store on your machine.
  • Projects get hard links to the files in that store, so 10 React projects can share a single copy of React on disk. Installs are fast because already stored files are not downloaded or copied again.
  • Its node_modules is strict: only the packages you declare appear at the top level, and dependencies live in node_modules/.pnpm, connected with symlinks.
  • So you can only import packages you've explicitly declared. No phantom dependencies.
  • It locks versions in pnpm-lock.yaml and has strong built-in support for monorepos (workspaces).

Bun

  • Bun is an all-in-one JavaScript toolkit: runtime, bundler, test runner, and package manager in a single binary.
  • bun install works with your existing package.json and installs into a regular node_modules, so you can use it as a package manager even if your app still runs on Node.js.
  • It uses a global cache and fast system calls to link or copy files. Bun claims installs can be up to 25× faster than npm install.
  • It writes bun.lock (a text lock file; older Bun versions used the binary bun.lockb).

Comparison

npmYarnpnpmBun
Lock filepackage-lock.jsonyarn.lockpnpm-lock.yamlbun.lock
Install layoutFlat node_modulesnode_modules (v1) or PnP (v2+)Strict, symlinked node_modulesnode_modules
Disk sharing across projectsSeparate copy per projectShared cache (PnP)Shared store + hard linksGlobal cache
Phantom dependenciesPossibleBlocked in PnPBlockedPossible with a hoisted layout
Comes with Node.jsYesNoNoNo (separate runtime)
Best known forDefault, zero setupPnP, zero-installsDisk efficiency, monoreposSpeed, all-in-one toolkit

Command cheat sheet

TasknpmYarnpnpmBun
Install everythingnpm installyarnpnpm installbun install
Add a packagenpm install reactyarn add reactpnpm add reactbun add react
Add a dev dependencynpm install -D vitestyarn add -D vitestpnpm add -D vitestbun add --dev vitest
Remove a packagenpm uninstall reactyarn remove reactpnpm remove reactbun remove react
Run a scriptnpm run devyarn devpnpm devbun run dev
Run a package oncenpx create-viteyarn dlx create-vite (v2+)pnpm dlx create-vitebunx create-vite
CI install from lock filenpm ciyarn install --immutable (v1: --frozen-lockfile)pnpm install --frozen-lockfilebun install --frozen-lockfile

Important qualifications

  • Commit the lock file, and use one package manager per project. Mixing them creates multiple lock files that disagree.
  • Declare the package manager in package.json with the packageManager field (for example, "packageManager": "pnpm@10.0.0") so the team and CI use the same tool and version.
  • Use the frozen install in CI so the build fails if package.json and the lock file are out of sync, instead of silently resolving new versions.
  • Phantom dependencies are a bug even when they work. If you import it, add it to package.json.
  • Install scripts are a security risk. Packages can run postinstall scripts on your machine. pnpm (v10+) and Bun don't run dependency lifecycle scripts by default unless you allow them.
  • Speed benchmarks vary. Results depend on cache state, network, project size, and operating system. Measure on your own project before switching.

Takeaway

  • npm: the default, bundled with Node.js, flat node_modules, possible phantom dependencies.
  • Yarn: introduced lock files by default; modern Yarn's Plug'n'Play replaces node_modules with a .pnp.cjs map.
  • pnpm: one shared copy per package version via hard links, with a strict layout and no phantom dependencies.
  • Bun: an all-in-one toolkit whose package manager focuses on raw install speed.

References