πŸ“‹ Cheat Sheets

pnpm Cheat Sheet β€” Commands, Workspaces, and Migration from npm


Click any command to expand the explanation and examples.

πŸ“¦ Install & Manage Packages

pnpm install / add / remove basics
# Install all dependencies
pnpm install
pnpm i                          # Shorthand

Add a package

pnpm add express pnpm add -D typescript # Dev dependency pnpm add -g nodemon # Global

Remove

pnpm remove express pnpm rm express # Shorthand

Update

pnpm update pnpm update express # Specific package pnpm update β€”latest # Ignore semver ranges

Check outdated

pnpm outdated

npm β†’ pnpm translation migrate
# npm                    β†’ pnpm
npm install              β†’ pnpm install
npm install express      β†’ pnpm add express
npm install -D jest      β†’ pnpm add -D jest
npm uninstall express    β†’ pnpm remove express
npm run build            β†’ pnpm build (or pnpm run build)
npm test                 β†’ pnpm test
npx create-next-app      β†’ pnpm create next-app
npm ci                   β†’ pnpm install --frozen-lockfile
npm cache clean --force  β†’ pnpm store prune

▢️ Scripts

pnpm run / exec scripts
# Run a script from package.json
pnpm run build
pnpm build              # Shorthand (no "run" needed)
pnpm dev
pnpm test

Run a binary from node_modules

pnpm exec eslint src/ pnpm dlx create-next-app # Like npx (download and run)

Pass args to scripts

pnpm test β€” β€”watch pnpm build β€” β€”mode production

πŸ—οΈ Workspaces (Monorepo)

Workspace setup and commands workspace
# pnpm-workspace.yaml (in repo root)
packages:
  - 'apps/*'
  - 'packages/*'

Structure:

my-monorepo/

pnpm-workspace.yaml

apps/

web/package.json

api/package.json

packages/

shared/package.json

ui/package.json

# Run command in specific workspace
pnpm --filter web dev
pnpm --filter api build

# Run in all workspaces
pnpm -r build
pnpm -r test

# Add dependency to specific workspace
pnpm --filter web add react

# Add workspace package as dependency
pnpm --filter web add @myorg/shared --workspace

# Install all workspaces
pnpm install

πŸ”§ Configuration

.npmrc settings for pnpm config
# .npmrc
# Auto-install peers (recommended)
auto-install-peers=true

Hoist to root node_modules (needed for some tools)

shamefully-hoist=true

Strict peer dependencies

strict-peer-dependencies=false

Use specific Node version

use-node-version=20.11.0

Store management config
pnpm uses a content-addressable store β€” packages are stored once globally and hard-linked into projects. This saves massive disk space.
# Show store path
pnpm store path

Clean unused packages from store

pnpm store prune

Check store status

pnpm store status

⚑ Why pnpm?

pnpm vs npm vs yarn comparison
# Disk space: pnpm stores packages once, links them
# npm: each project gets its own copy of every package
# pnpm: one copy shared across all projects

Speed: pnpm is faster because it links instead of copying

npm install: ~30s (typical project)

pnpm install: ~10s (same project)

Strictness: pnpm doesn’t hoist by default

This means you can’t accidentally import packages

you didn’t declare as dependencies (phantom dependencies)