All cheatsheetsFrontend Development

yarn

Yarn 1.x (classic) / Yarn 4 (berry)

Quick reference for everyday Yarn CLI commands (both Yarn Classic 1.x and Yarn Berry 2/3/4): install / add / remove / run / workspaces, lockfile, Plug'n'Play, and Berry configuration — with common options and practical examples.

33 commands

Install (Classic 1.x)

yarn install

Install every dependency listed in `package.json`, creating or refreshing `node_modules` and `yarn.lock`.

--frozen-lockfile fail if lock would change (CI); --production / --prod skip devDeps; --no-progress; --silent

yarn install --frozen-lockfile
yarn install --frozen-lockfile

Install only if `yarn.lock` matches `package.json` exactly. Used in CI to guarantee reproducible builds.

yarn install --frozen-lockfile

Install (Berry)

yarn install

Berry default: uses Plug'n'Play when `nodeLinker: pnp` is in `.yarnrc.yml`, otherwise falls back to `node_modules`. Caches in `.yarn/cache`.

--immutable fail on lockfile drift (Berry equivalent of --frozen-lockfile); --inline-builds; --mode=update-lockfile

yarn install --immutable
yarn set version <version>

Switch the Yarn binary in the project. Common: `yarn set version stable`, `yarn set version 4.0.2`, `yarn set version classic`.

yarn set version stable

Add dependency

yarn add <pkg>

Add a runtime dep and write to `package.json` + lockfile. Classic writes to `dependencies`, Berry same.

<pkg>@<version|range>; --dev / -D; --exact / -E; --optional / -O; --peer

yarn add lodash && yarn add -D typescript@^5 vitest
yarn add -D <pkg>

Add to `devDependencies`.

yarn add -D typescript @types/node eslint
yarn add <pkg>@<version>

Install a specific version, dist-tag (`@latest`, `@next`, `@canary`), or git URL.

yarn add [email protected] && yarn add next@latest
yarn global add <pkg>

Install a tool globally (Classic 1.x). Berry uses `yarn dlx` for one-shot binaries and recommends `corepack` for global tools.

yarn global add typescript

Remove

yarn remove <pkg>

Remove a package from `node_modules` and the relevant `dependencies` / `devDependencies` section.

yarn remove lodash && yarn remove -D @types/node

Update

yarn upgrade

Upgrade all dependencies within their current semver ranges; `yarn upgrade --latest` jumps to the newest available release and rewrites the range.

--latest; --pattern <glob>; --caret; --exact; -i / --interactive

yarn upgrade && yarn upgrade eslint --latest
yarn upgrade <pkg>@<version>

Upgrade a single package to a specific version.

yarn upgrade [email protected]

Run script

yarn run <script>

Run a script from `package.json`. `yarn test`, `yarn start`, `yarn build` are shorthand.

-- <args> forward args to the script

yarn run build -- --mode production
yarn <script>

Same as `yarn run <script>` — call any defined script by name without the `run` keyword.

yarn test && yarn lint:fix
yarn dlx <pkg> [args]

Run a package binary on demand without installing it locally (Berry equivalent of `npx`). Caches to `~/.yarn/berry/cache`.

-p / --package pick package; -q / --quiet

yarn dlx create-vite my-app --template vue

Workspaces

yarn workspaces add <pkg>

Add a dependency to the root of a monorepo workspace (1.x).

yarn workspaces add typescript -D
yarn workspace <ws> add <pkg>

Add a dep to a single named workspace within the monorepo.

yarn workspace @myorg/web add react && yarn workspace @myorg/api add -D vitest
yarn workspaces run <script>

Run a script in every workspace. `yarn workspaces foreach run <script>` (Berry) supports concurrency and topology order.

--parallel; --topological; --continue; --interlaced

yarn workspaces foreach run build --topological --parallel

Inspect

yarn list

Print the dependency tree. `--pattern <regex>` filters; `--depth 0` shows direct deps only.

--depth=N; --pattern; --json

yarn list --depth=0 && yarn list --pattern "^react$"
yarn why <pkg>

Explain why a given package is installed — the chain of dependencies that brought it in.

yarn why react && yarn why @types/node
yarn info <pkg>

Print registry metadata for a package — versions, dependencies, dist-tags, readme excerpt. Roughly equivalent to `npm view`.

<pkg>@<version>; <pkg> readme; <pkg> dependencies

yarn info react dist-tags && yarn info lodash versions
yarn licenses list

List every license across the dependency tree (SPDX identifier + author). Useful for license-compliance reviews.

yarn licenses list

Berry configuration

yarn config set <key> <value>

Write a config entry to `.yarnrc.yml` (Berry). Common keys: `nodeLinker`, `npmRegistryServer`, `enableTelemetry`, `yarnPath`.

yarn config set nodeLinker node-modules && yarn config set enableTelemetry false
yarn config get <key>

Read a single config entry.

yarn config get nodeLinker

Cache

yarn cache clean

Wipe the Yarn cache (Classic: `~/.yarn-cache`; Berry: `.yarn/cache`).

yarn cache clean
yarn cache dir

Print the cache directory path.

yarn cache dir

Lockfile

yarn import

Berry only — import `package-lock.json` from npm into Yarn's lockfile format.

yarn import
yarn constraints

Berry only — enforce project-wide dependency rules written in Prolog in `constraints.pro` (e.g. every workspace must depend on the same React version).

yarn constraints

Audit

yarn npm audit

Berry — proxy to the npm registry's advisory feed. Reports vulnerabilities in the lockfile.

--severity info|low|moderate|high|critical; --json

yarn npm audit --severity high

Misc

yarn version

Print the current Yarn version (works for both Classic and Berry).

yarn version
yarn init

Create a `package.json` interactively or with `-y` for defaults.

-y accept defaults; -p / --private mark as private

yarn init -y
yarn pack

Bundle the current package into a tarball without publishing — useful for offline distribution.

--filename; --pack-destination <dir>

yarn pack --filename my-pkg.tgz

Plug'n'Play (Berry)

yarn unplug <pkg>

Extract a single package from the PnP zip store into a normal `node_modules/<pkg>` so it can be required the old-fashioned way (workaround for incompatible tooling).

yarn unplug react && yarn install
yarn rebuild

Recompile native addons for the current Node ABI. Berry needs this for packages with native deps under PnP.

yarn rebuild

Related command cheatsheets

About Yarn

Yarn was released by Facebook (Meta) in October 2016 as a faster, more deterministic alternative to npm, alongside contributions from Google, Exponent, and Tilde. The original 1.x line is now called "Yarn Classic" and is in maintenance mode; the active line is "Yarn Berry" (2.0 in 2020, 3.0 in 2021, 4.0 in 2022, 5.x today). Yarn Berry ships `yarn` (a single 4 MB binary with Node embedded), `yarn dlx` (npx replacement), `yarn workspaces`, and Plug'n'Play (`yarn install --immutable` plus `.pnp.cjs`) — a controversial default that installs no `node_modules` and resolves packages from a single zip store. Berry is configured in a `yarnrc.yml` file (YAML, unlike Classic's `yarnrc` ini). Both lines coexist on the same machine: `yarn set version classic` and `yarn set version berry` switch the binary. Yarn is faster than npm at install (parallel + cache), supports `workspaces` and `constraints` for monorepo policy, and has built-in `yarn why` / `yarn licenses` introspection. No data is sent to Meta — Yarn is independent OSS under BSD-2-Clause.

Cheatsheet version 1.0.0