npm → Yarn
The npm command you know beside its Yarn equivalent, for every common task. Covers both Yarn 1 (Classic) and Yarn 2+ (Berry), which differ on most of the interesting ones.
Runs in your browser
| Task | npm | yarn |
|---|---|---|
| Install everything in the lockfile | ||
| Install for CI, failing if the lockfile would change | Yarn 1 (Classic): | |
| Add a dependency | ||
| Add a dev dependency | ||
| Add without a version range | ||
| Install globally | no equivalent Yarn 1 (Classic): | |
| Remove a dependency | ||
| Update within the declared ranges | Yarn 1 (Classic): | |
| List packages behind their latest | Yarn 1 (Classic): | |
| Run a script from package.json | ||
| Run a package binary without installing it | ||
| Start a new package | ||
| Explain why a package is installed | ||
| Check dependencies for advisories | Yarn 1 (Classic): | |
| Add a dependency to one workspace |
Yarn columns are Yarn 2+ (Berry). Where Yarn 1 differs, it is shown underneath.
The short version
| npm | Yarn |
|---|---|
npm install | yarn install — or just yarn |
npm install <pkg> | yarn add <pkg> |
npm install -D <pkg> | yarn add -D <pkg> |
npm uninstall <pkg> | yarn remove <pkg> |
npm run build | yarn build |
npx <pkg> | yarn dlx <pkg> — Berry only |
The full table is above, with your package name filled in.
Which Yarn?
This matters more than the command list. yarn --version will tell you.
Yarn 1.x (Classic) is the one most tutorials were written for and is in maintenance. Its
lockfile is a custom format, and it installs a flat node_modules like npm.
Yarn 2+ (Berry) is a different program with the same name. By default it uses Plug’n’Play
— no node_modules at all, dependencies resolved from a single .pnp.cjs map — and it changed
several commands. corepack enable plus a packageManager field in package.json is the
current way to pin which one a project uses.
Where the two disagree, the table above shows Berry on the line and Classic underneath.
Three things npm does that Yarn does differently
npm ci has no exact counterpart. yarn install --immutable (Berry) and
yarn install --frozen-lockfile (Classic) both fail rather than update the lockfile, which is
the property CI needs. Neither wipes node_modules first, which npm ci does.
Yarn 2+ removed global installs. yarn global add works in Classic and does not exist in
Berry, and nothing replaced it. If a tutorial tells you to install something globally with
Yarn, check your version before you go looking for the flag.
yarn build runs your script; npm build does not. npm needs npm run build. npm build
is a separate deprecated command and will not run the build entry in your scripts.
Migrating a project
- Delete
package-lock.jsonandnode_modules. - Run
yarn install. - Read the diff. Yarn’s resolver is not npm’s, and a range like
^1.2.0can legitimately land on a different version. - Commit
yarn.lock, and onlyyarn.lock.
Two lockfiles in a repository is how a team ends up with two dependency trees depending on who ran which command.
Related
npm to pnpm, or the full four-way table including Bun.