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_modulesfolder 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 ownpackage-lock.json(npm 5, 2017). - Yarn Classic (v1) still uses
node_modulesand 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_modulesfolder. - Yarn generates a
.pnp.cjsfile 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 withnodeLinker: node-modules.
- There is no
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_modulesis strict: only the packages you declare appear at the top level, and dependencies live innode_modules/.pnpm, connected with symlinks. - So you can only import packages you've explicitly declared. No phantom dependencies.
- It locks versions in
pnpm-lock.yamland 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 installworks with your existingpackage.jsonand installs into a regularnode_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 binarybun.lockb).
Comparison
| npm | Yarn | pnpm | Bun | |
|---|---|---|---|---|
| Lock file | package-lock.json | yarn.lock | pnpm-lock.yaml | bun.lock |
| Install layout | Flat node_modules | node_modules (v1) or PnP (v2+) | Strict, symlinked node_modules | node_modules |
| Disk sharing across projects | Separate copy per project | Shared cache (PnP) | Shared store + hard links | Global cache |
| Phantom dependencies | Possible | Blocked in PnP | Blocked | Possible with a hoisted layout |
| Comes with Node.js | Yes | No | No | No (separate runtime) |
| Best known for | Default, zero setup | PnP, zero-installs | Disk efficiency, monorepos | Speed, all-in-one toolkit |
Command cheat sheet
| Task | npm | Yarn | pnpm | Bun |
|---|---|---|---|---|
| Install everything | npm install | yarn | pnpm install | bun install |
| Add a package | npm install react | yarn add react | pnpm add react | bun add react |
| Add a dev dependency | npm install -D vitest | yarn add -D vitest | pnpm add -D vitest | bun add --dev vitest |
| Remove a package | npm uninstall react | yarn remove react | pnpm remove react | bun remove react |
| Run a script | npm run dev | yarn dev | pnpm dev | bun run dev |
| Run a package once | npx create-vite | yarn dlx create-vite (v2+) | pnpm dlx create-vite | bunx create-vite |
| CI install from lock file | npm ci | yarn install --immutable (v1: --frozen-lockfile) | pnpm install --frozen-lockfile | bun 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.jsonwith thepackageManagerfield (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.jsonand 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
postinstallscripts 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_moduleswith a.pnp.cjsmap. - 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.