npm vs pnpm vs Yarn Cheat Sheet: Commands Side by Side
Checked against npm, pnpm and Yarn npm 12.2.3, pnpm 12.23.0 and Yarn 4.19.0 docs; npm rows run on npm 11.4.2 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet lines up npm, pnpm and Yarn (the modern Berry line, not Yarn 1) so you can translate a command from one project to the next. It follows the npm 12.2.3, pnpm 12.23.0 and Yarn 4.19.0 documentation. The npm rows were run on npm 11.4.2 in a scratch project; pnpm and Yarn weren't installed on the test machine, so their rows are marked as checked in the docs only.
When to use what
If the repository already has a lockfile, use the manager that wrote it: package-lock.json means npm, pnpm-lock.yaml means pnpm and yarn.lock means Yarn. Mixing them gives you two lockfiles that disagree, and CI will install whichever one its script happens to call.
For a new project, npm is the safe default because it ships with Node and every tutorial assumes it. I reach for pnpm in monorepos: the --filter selectors and the workspace: protocol make it easy to build one package and its dependencies without touching the rest, and its strict node_modules layout catches packages that import things they never declared. Yarn 4 is a good fit when a team already likes Plug'n'Play or wants yarn workspaces foreach with topological ordering built in.
In CI, always use the frozen form: npm ci, pnpm install --frozen-lockfile or yarn install --immutable. pnpm and Yarn switch to this behaviour on their own when they detect CI, but writing the flag makes the intent obvious to the next person. The package manager comparison post covers speed and disk use in more depth, and the package.json generator gives you a clean starting manifest.
pnpm: https://pnpm.io/cli/add and https://pnpm.io/cli/install. Yarn: https://yarnpkg.com/cli/add. npm install also answers to npm add and npm i.
npm installInstall everything in package.json and update package-lock.json if needed
npm 12 blocks dependency install scripts unless allowScripts approves them
npm install lodashAdd a runtime dependency with a caret range such as ^4.17.20
npm install -D semverAdd a package to devDependencies
npm uninstall lodashRemove a package from node_modules, package.json and the lockfile
pnpm add -D saxAdd a dev dependency with pnpm
pnpm remove saxRemove a dependency with pnpm
yarn add -D typescriptAdd a dev dependency with Yarn 4 (-D is short for --dev)
yarn remove typescriptRemove a dependency with Yarn
pnpm's CI behaviour is described at https://pnpm.io/cli/install and Yarn's at https://yarnpkg.com/cli/install. The lockfile names are package-lock.json, pnpm-lock.yaml and yarn.lock; commit the one your manager writes.
npm ciDelete node_modules and install exactly what package-lock.json says; fails if it disagrees with package.json
npm 12 also blocks unapproved dependency install scripts here
pnpm install --frozen-lockfileInstall without touching pnpm-lock.yaml and fail if it needs an update (the default in CI)
pnpm install --no-frozen-lockfileAllow pnpm to rewrite the lockfile when running in CI
yarn install --immutableFail instead of changing yarn.lock (on by default in CI)
yarn install --immutable --immutable-cache --check-cacheStrict Yarn install that also verifies the committed cache, for Zero-Installs repos
pnpm: https://pnpm.io/cli/dlx and https://pnpm.io/cli/exec. Yarn: https://yarnpkg.com/cli/run and https://yarnpkg.com/cli/dlx. exec runs a binary already in your dependencies; dlx and npx fetch one into a temporary place.
npm run helloRun the hello entry from the scripts field, printing the command first
npm exec -- semver 1.2.3 -i minorRun a binary from local node_modules (printed 1.3.0 here)
npx semver --helpSame as npm exec; downloads the package if it isn't installed
pnpm exec eslint .Run a binary from the project's dependencies with pnpm
pnpm dlx create-viteDownload and run a package without adding it (alias pnpx)
yarn run buildRun a package.json script with Yarn
yarn dlx create-viteRun a package in a temporary environment with Yarn
See also https://docs.npmjs.com/cli/v12/commands/npm-outdated, https://pnpm.io/cli/update and https://yarnpkg.com/cli/up. Yarn 4 has no outdated command; upgrade-interactive fills that gap.
npm outdatedTable of Current, Wanted (highest in range) and Latest versions for each dependency
npm updateMove packages to the newest version their range allows; package.json ranges stay as they were
npm update --saveAlso rewrite the ranges in package.json to the new versions
pnpm outdatedList dependencies that have newer versions with pnpm
pnpm up --latestUpdate every dependency to its latest version, ignoring the current ranges
yarn up reactUpgrade one package to its latest version across all workspaces
yarn upgrade-interactivePick upgrades from an interactive list in Yarn
See also https://docs.npmjs.com/cli/v12/commands/npm-explain, https://pnpm.io/cli/why, https://pnpm.io/cli/audit, https://yarnpkg.com/cli/why and https://yarnpkg.com/cli/npm/audit.
npm explain lodashShow the chain of dependents that pulled a package in (npm why is an alias)
npm auditReport known advisories for installed packages, grouped by package and severity
npm audit fixApply the non-breaking upgrades that resolve advisories
pnpm why reactShow which packages depend on a package in a pnpm project
pnpm audit --fixAdd overrides to the workspace config that force patched versions
yarn why lodashExplain why a package is in a Yarn project
yarn npm audit --allAudit dependencies of every workspace in a Yarn monorepo
pnpm reads workspaces from pnpm-workspace.yaml (https://pnpm.io/workspaces, filters at https://pnpm.io/filtering). Yarn reads the workspaces field (https://yarnpkg.com/features/workspaces). Both pnpm and Yarn support "dep": "workspace:^" style ranges for local packages.
npm init -y -w packages/aCreate a workspace package and add it to the root workspaces array
npm run build -w packages/aRun a script in one workspace
npm run build --workspaces --if-presentRun a script in every workspace and skip the ones without it
pnpm --filter @app/web buildRun a script only in the packages matched by the selector (-F for short)
pnpm -r updateRun update in every workspace package
yarn workspaces foreach -A run buildRun a script in all Yarn workspaces
yarn workspaces foreach -Apt run buildBuild all workspaces in parallel, dependencies first
Version bumps: https://docs.npmjs.com/cli/v12/commands/npm-version. pnpm: https://pnpm.io/cli/publish. Yarn: https://yarnpkg.com/cli/npm/publish. The publishing walkthrough covers the package.json fields to check first.
npm version patch --no-git-tag-versionBump 1.0.0 to 1.0.1 in package.json without a git commit or tag
npm version premajor --preid=rc --no-git-tag-versionBump to the next major as a pre-release, here v2.0.0-rc.0
npm publish --dry-runPack and report what would be uploaded without publishing
npm publish --tag nextPublish under a dist-tag other than latest; required for pre-release versions
pnpm publish --new-version 1.4.0Set the version in package.json and publish in one step (pnpm 12.23)
yarn npm publish --tag betaPublish the current workspace from Yarn under a dist-tag
Worked example: Find a vulnerable package, see why it's there, and fix it in CI
Run on npm 11.4.2 (npm 12 also blocks dependency install scripts by default). Start in a project with an old lodash and an old semver as a dev dependency, then ask npm what is stale and what is unsafe:
$ npm install [email protected]
$ npm install -D [email protected]
$ npm outdated
Package Current Wanted Latest Location Depended by
lodash 4.17.20 4.18.1 4.18.1 node_modules/lodash npmtest
semver 7.5.0 7.8.6 7.8.6 node_modules/semver npmtest
$ npm explain lodash
[email protected]
node_modules/lodash
lodash@"^4.17.20" from the root project
$ npm audit
# npm audit report
lodash <=4.17.23
Severity: high
Wanted is the highest version your range in package.json allows; Latest is the registry's latest tag. When the two match, a plain npm update gets you there. npm explain tells you lodash is a direct dependency, so you control the fix yourself rather than waiting on another package.
$ npm update
$ npm ls semver
└── [email protected]
$ grep '"semver"' package.json
"semver": "^7.5.0"
The installed versions moved but the ranges in package.json didn't, which is the documented default for npm update. Commit the changed lockfile, and in CI run npm ci so the pipeline installs exactly those versions.
Common mistakes
Running
npm installin CI and committing whatever lockfile changes it makesUse
npm ci,pnpm install --frozen-lockfileoryarn install --immutableso a drifted lockfile fails the build instead of being silently rewritten.Keeping both package-lock.json and yarn.lock (or pnpm-lock.yaml) in one repository
Pick one manager, delete the other lockfiles, and pin the choice with the
packageManagerfield so Corepack and teammates use the same tool.Expecting
npm updateto bump the ranges in package.jsonBy default it only changes node_modules and the lockfile. Pass
--saveif you want the ranges rewritten, or edit them and runnpm install.Publishing a pre-release with plain
npm publishnpm refuses a pre-release version without a tag. Publish with
--tag next(orbeta) so people onlatestdon't get the pre-release by accident.Upgrading to npm 12 and finding that a native module no longer builds after install
npm 12 blocks dependency install scripts by default. Review them with
npm install-scripts approve, then runnpm rebuild; record the decision inallowScripts.Using
npx some-clifor a tool your build depends onAdd it as a dev dependency and call it from a script, so the version is locked; keep
npxanddlxfor one-off generators.
Related cheat sheets
Sources
- https://docs.npmjs.com/cli/v12/commands/npm-ci
- https://docs.npmjs.com/cli/v12/commands/npm-install
- https://docs.npmjs.com/cli/v12/using-npm/workspaces
- https://github.com/npm/cli/releases/tag/v12.0.0
- https://pnpm.io/cli/install
- https://pnpm.io/filtering
- https://github.com/pnpm/pnpm/releases/tag/v12.0.0
- https://github.com/pnpm/pnpm/releases/tag/v12.23.0
- https://yarnpkg.com/cli
- https://yarnpkg.com/cli/workspaces/foreach
- https://yarnpkg.com/getting-started/qa
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.