Skip to content

Menu

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 install

Install everything in package.json and update package-lock.json if needed

npm 12 blocks dependency install scripts unless allowScripts approves them

From the docs, not run by us
npm install lodash

Add a runtime dependency with a caret range such as ^4.17.20

From the docs, not run by us
npm install -D semver

Add a package to devDependencies

From the docs, not run by us
npm uninstall lodash

Remove a package from node_modules, package.json and the lockfile

pnpm add -D sax

Add a dev dependency with pnpm

From the docs, not run by us
pnpm remove sax

Remove a dependency with pnpm

From the docs, not run by us
yarn add -D typescript

Add a dev dependency with Yarn 4 (-D is short for --dev)

From the docs, not run by us
yarn remove typescript

Remove a dependency with Yarn

From the docs, not run by us

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 ci

Delete 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

From the docs, not run by us
pnpm install --frozen-lockfile

Install without touching pnpm-lock.yaml and fail if it needs an update (the default in CI)

From the docs, not run by us
pnpm install --no-frozen-lockfile

Allow pnpm to rewrite the lockfile when running in CI

From the docs, not run by us
yarn install --immutable

Fail instead of changing yarn.lock (on by default in CI)

From the docs, not run by us
yarn install --immutable --immutable-cache --check-cache

Strict Yarn install that also verifies the committed cache, for Zero-Installs repos

From the docs, not run by us

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 hello

Run the hello entry from the scripts field, printing the command first

npm exec -- semver 1.2.3 -i minor

Run a binary from local node_modules (printed 1.3.0 here)

npx semver --help

Same as npm exec; downloads the package if it isn't installed

pnpm exec eslint .

Run a binary from the project's dependencies with pnpm

From the docs, not run by us
pnpm dlx create-vite

Download and run a package without adding it (alias pnpx)

From the docs, not run by us
yarn run build

Run a package.json script with Yarn

From the docs, not run by us
yarn dlx create-vite

Run a package in a temporary environment with Yarn

From the docs, not run by us

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 outdated

Table of Current, Wanted (highest in range) and Latest versions for each dependency

npm update

Move packages to the newest version their range allows; package.json ranges stay as they were

From the docs, not run by us
npm update --save

Also rewrite the ranges in package.json to the new versions

From the docs, not run by us
pnpm outdated

List dependencies that have newer versions with pnpm

From the docs, not run by us
pnpm up --latest

Update every dependency to its latest version, ignoring the current ranges

From the docs, not run by us
yarn up react

Upgrade one package to its latest version across all workspaces

From the docs, not run by us
yarn upgrade-interactive

Pick upgrades from an interactive list in Yarn

From the docs, not run by us

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 lodash

Show the chain of dependents that pulled a package in (npm why is an alias)

npm audit

Report known advisories for installed packages, grouped by package and severity

npm audit fix

Apply the non-breaking upgrades that resolve advisories

From the docs, not run by us
pnpm why react

Show which packages depend on a package in a pnpm project

From the docs, not run by us
pnpm audit --fix

Add overrides to the workspace config that force patched versions

From the docs, not run by us
yarn why lodash

Explain why a package is in a Yarn project

From the docs, not run by us
yarn npm audit --all

Audit dependencies of every workspace in a Yarn monorepo

From the docs, not run by us

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/a

Create a workspace package and add it to the root workspaces array

npm run build -w packages/a

Run a script in one workspace

npm run build --workspaces --if-present

Run a script in every workspace and skip the ones without it

pnpm --filter @app/web build

Run a script only in the packages matched by the selector (-F for short)

From the docs, not run by us
pnpm -r update

Run update in every workspace package

From the docs, not run by us
yarn workspaces foreach -A run build

Run a script in all Yarn workspaces

From the docs, not run by us
yarn workspaces foreach -Apt run build

Build all workspaces in parallel, dependencies first

From the docs, not run by us

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-version

Bump 1.0.0 to 1.0.1 in package.json without a git commit or tag

npm version premajor --preid=rc --no-git-tag-version

Bump to the next major as a pre-release, here v2.0.0-rc.0

npm publish --dry-run

Pack and report what would be uploaded without publishing

npm publish --tag next

Publish under a dist-tag other than latest; required for pre-release versions

From the docs, not run by us
pnpm publish --new-version 1.4.0

Set the version in package.json and publish in one step (pnpm 12.23)

From the docs, not run by us
yarn npm publish --tag beta

Publish the current workspace from Yarn under a dist-tag

From the docs, not run by us

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 install in CI and committing whatever lockfile changes it makes

    Use npm ci, pnpm install --frozen-lockfile or yarn install --immutable so 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 packageManager field so Corepack and teammates use the same tool.

  • Expecting npm update to bump the ranges in package.json

    By default it only changes node_modules and the lockfile. Pass --save if you want the ranges rewritten, or edit them and run npm install.

  • Publishing a pre-release with plain npm publish

    npm refuses a pre-release version without a tag. Publish with --tag next (or beta) so people on latest don'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 run npm rebuild; record the decision in allowScripts.

  • Using npx some-cli for a tool your build depends on

    Add it as a dev dependency and call it from a script, so the version is locked; keep npx and dlx for one-off generators.