Cheat Sheet Library

npm / pnpm / yarn Cheat Sheet

The three JavaScript package managers side by side. Yarn commands are for Yarn 2+ (Berry) unless marked v1. Enable pnpm/yarn with corepack enable.

  • 39 entries
  • 7 sections
  • Updated
  • Click a command to see all its options

39 entries

On this page

Side by Side #

Tasknpmpnpmyarn
New project
Install all deps
Add a package
Add a dev dependency
Add a specific version
Install globally (v1)
Remove
Run a script
Run a package once
Update deps
Update to latest (ignore ranges)
Show outdated
CI install (lockfile only)
Why is this installed?
List installed
Security audit
Publish
Lockfilepackage-lock.jsonpnpm-lock.yamlyarn.lock

npm Extras #

CommandAction
List available scripts
/ Log in to the npm registry
/ minor / majorBump the version + git tag
Publish a scoped package (@you/pkg) publicly
See which files would be published
Clear the cache
Show the registry
All published versions
Scaffold a project with a template

Version Ranges in package.json #

RangeAllows
"1.2.3"Exactly 1.2.3
"^1.2.3"1.x.x (≥1.2.3, <2.0.0), the default
"~1.2.3"1.2.x (≥1.2.3, <1.3.0)
"*" / "latest"Anything (avoid)

Workspaces (monorepo) #

Tasknpmpnpmyarn
Define"workspaces": ["packages/*"] in package.jsonpnpm-workspace.yaml"workspaces" in package.json
Run in one package
Run in all

Node Versions #

CommandAction
/ Versions
/ nvm (macOS/Linux; nvm-windows is similar)
/ fnm (fast, cross-platform)
"engines": { "node": ">=20" }Declare the Node version in package.json

Fix Weird Install Problems #

rm -rf node_modules package-lock.json   # or pnpm-lock.yaml / yarn.lock
npm cache clean --force
npm install

Tips #

  • Commit the lockfile. Never commit node_modules.
  • npm run scripts can call binaries in node_modules/.bin directly ("lint": "eslint .").
  • pnpm saves a lot of disk space by sharing one package store between projects.