npm / yarn / pnpm Cheatsheet

Same operations, three package managers. Pick one per project and stick with it.

1 credit

Install

6 items
All deps
npm install / yarn / pnpm install
Single dep
npm i react / yarn add react / pnpm add react
Dev dep
npm i -D eslint / yarn add -D / pnpm add -D
Global
npm i -g pm2 / yarn global add / pnpm add -g
Exact version
npm i react@18.2.0 --save-exact
Clean reinstall
rm -rf node_modules lockfile && install

CI-safe install

2 items
Fast, uses lock exactly
npm ci / yarn install --frozen-lockfile / pnpm install --frozen-lockfile
Skip optional / dev
npm ci --omit=dev --omit=optional

Scripts & running

3 items
Run script
npm run dev / yarn dev / pnpm dev
Args to script
npm run test -- --watch (yarn/pnpm drop the --)
Run a binary w/o install
npx cowsay hi / pnpm dlx cowsay hi

Inspect & update

4 items
Outdated deps
npm outdated / yarn outdated / pnpm outdated
Audit vulnerabilities
npm audit / yarn npm audit / pnpm audit
Why is pkg installed
npm ls <pkg> / yarn why <pkg> / pnpm why <pkg>
Upgrade interactively
npx npm-check-updates -i

Further reading