npm vs yarn vs pnpm Cheat Sheet
Author
Lorenzo Bianchi
Date Published

Guide to JavaScript Package Managers: npm, yarn, and pnpm
Introduction
npm, yarn, and pnpm are the three main package managers for JavaScript and Node.js. Each of them allows you to install, manage, and update your project dependencies, but with slightly different approaches and philosophies.
npm is installed by default when we install Node.js on our system.
yarn, initially developed by Facebook, was created as an alternative to npm to offer greater speed and reliability.
pnpm (performant npm) is a more recent package manager that focuses on disk space efficiency and speed, using an innovative approach to module management.
Installation
npm
No installation required, it's already included with Node.js.
yarn
To install yarn globally:
npm install -g yarn
Currently, two main versions are available: yarn v1 (classic) and yarn v2+ (modern with advanced features). Check the official yarn documentation for details on migrating between versions.
pnpm
To install pnpm globally:
npm install -g pnpm
Alternatively, you can use curl on macOS or Linux:
curl -fsSL https://get.pnpm.io/install.sh | sh -
Advantages and Features
npm
- Official manager for Node.js
- Large community and universal compatibility
- Simple and intuitive package management
- Continuous improvements in recent versions
yarn
- Superior performance thanks to caching
- Deterministic locking with
yarn.lockfile - Workspaces for monorepo management
- More intuitive commands compared to npm
pnpm
- Space efficiency: uses a "content-addressable" approach to avoid duplicates
- Speed: faster than npm and yarn in many scenarios
- Strict integrity: prevents unauthorized access to undeclared dependencies
- Native monorepo support: built-in workspaces support
- Lower disk consumption: drastically reduces space occupied by
node_modules
Main Commands
| Command | npm | yarn | pnpm |
|---|---|---|---|
| Install dependencies | npm install | yarn install | pnpm install |
| Add package | npm install [package] | yarn add [package] | pnpm add [package] |
| Add dev dependency | npm install --save-dev [package] | yarn add [package] --dev | pnpm add --save-dev [package] |
| Add optional dependency | npm install --save-optional [package] | yarn add [package] --optional | pnpm add --optional [package] |
| Install specific version | npm install [package]@1.2.3 | yarn add [package]@1.2.3 | pnpm add [package]@1.2.3 |
| Remove package | npm uninstall [package] | yarn remove [package] | pnpm remove [package] |
| Update dependencies | npm update | yarn upgrade | pnpm update |
| Update specific package | npm update [package] | yarn upgrade [package] | pnpm update [package] |
| Global install | npm install -g [package] | yarn global add [package] | pnpm add -g [package] |
| Global uninstall | npm uninstall -g [package] | yarn global remove [package] | pnpm remove -g [package] |
| Run tests | npm test | yarn test | pnpm test |
| Run scripts | npm run [script] | yarn [script] | pnpm run [script] |
| Check outdated packages | npm outdated | yarn outdated | pnpm outdated |
| Clean cache | npm cache clean --force | yarn cache clean | pnpm store prune |
| Rebuild packages | npm rebuild | yarn add --force | pnpm rebuild |
| Login | npm login | yarn login | pnpm login |
Monorepo and Workspaces Management
All three managers support workspaces for managing monorepo projects, but with slightly different syntax.
npm: Configuration in package.json
1{2 "workspaces": [3 "packages/*"4 ]5}6
yarn: Configuration in package.json or .yarnrc.yml
1{2 "workspaces": [3 "packages/*"4 ]5}
pnpm: Configuration in pnpm-workspace.yaml
1packages:2 - 'packages/*'
Lock Files
All three managers create lock files to ensure reproducibility:
- npm:
package-lock.json - yarn:
yarn.lock - pnpm:
pnpm-lock.yaml
It's important to commit these files to version control to ensure that all developers and CI/CD systems use the same versions.
