Getting started

Introduction

Yarn is a package manager for JavaScript. This reference covers Yarn 4 (the Berry line): Corepack installs, the modern CLI, workspaces, constraints, and Plug'n'Play.

Install

corepack enable           # enable the version shim
yarn --version            # reads packageManager
yarn init -2              # start a Yarn 4 project
yarn set version stable   # move to the latest 4.x

Yarn 4 needs Node 18.12+. Corepack reads the packageManager field, so every contributor runs the pinned release.

See: Installation

Pinning the version

{
  "packageManager": "yarn@4.18.1"
}

Commit package.json; Corepack installs that exact version per project. Change it with corepack use yarn@4.18.1.

See: corepack

Migrating from Yarn 1

corepack enable
yarn set version stable
yarn install

Rename settings from .yarnrc and .npmrc into .yarnrc.yml, then commit the lockfile and config.

See: Migration guide

Everyday commands

npm equivalents

npmYarn 4
npm installyarn install
npm i lodash -Dyarn add -D lodash
npm uninstall lodashyarn remove lodash
npm updateyarn up
npm audityarn npm audit
npm publishyarn npm publish
npx create-viteyarn dlx create-vite
./node_modules/.bin/viteyarn run vite
npm install -g pkgyarn dlx pkg
npm install --productionyarn workspaces focus -A --production

yarn global was removed; use yarn dlx for one-off binaries.

See: CLI reference

Install

yarn install
yarn install --immutable
yarn install --immutable --immutable-cache
yarn install --mode=update-lockfile

--immutable fails if the lockfile would change, and defaults on in CI. --immutable-cache also fails when the cache changes.

See: yarn install

Add and remove

yarn add lodash
yarn add -D vitest
yarn add -E react
yarn add -P react
yarn remove lodash

-D is dev, -P is peer, -E is exact, -T is tilde. Yarn writes to the nearest workspace.

See: yarn add, yarn remove

Upgrade

yarn up lodash
yarn up -i
yarn up -R
yarn upgrade-interactive
yarn dedupe

yarn up ignores manifest ranges and updates every workspace; -i prompts, -R resolves again without touching manifests.

See: yarn up

Workspaces

Defining workspaces

{
  "workspaces": ["packages/*"]
}

Each match is a package with its own package.json; Yarn links them and installs once at the root.

See: Workspaces

Cross-references

{
  "dependencies": {
    "@my-org/utils": "workspace:^"
  }
}

workspace: resolves to the local package. workspace:^ becomes ^<version> after publishing.

See: Workspaces

Running scripts

yarn workspaces foreach -A run build
yarn workspaces foreach -A -pt run build
yarn workspaces focus @my-org/app
yarn workspaces focus -A --production

-A is all workspaces, -t is topological order, -p is parallel. focus installs one workspace's deps.

See: workspaces foreach

Constraints

// yarn.config.cjs
module.exports = {
  async constraints ({ Yarn }) {
    for (const dep of Yarn.dependencies({ ident: 'react' })) {
      dep.update('19.0.0')
    }
  }
}
yarn constraints
yarn constraints --fix

Constraints declare rules across workspaces; --fix rewrites manifests to satisfy them. Yarn 4 uses JavaScript rather than Prolog.

See: Constraints

Advanced

Plug'n'Play

.yarnrc.yml

nodeLinker: pnp

Yarn 4 resolves packages from zip archives through a .pnp.cjs loader, so no node_modules is created. Set nodeLinker: node-modules to opt out.

See: Plug'n'Play

Zero-install

.yarnrc.yml

enableGlobalCache: false

Committing .yarn/cache and .pnp.cjs makes a checkout behave like an install. Yarn 4 leaves zero-install off for new projects.

See: Cache strategies

dlx

yarn dlx create-vite
yarn dlx -p typescript -p ts-node ts-node -e "1 + 1"

dlx installs into a temporary environment and runs the binary in the current directory.

See: yarn dlx

Registry and config

yarn config get npmRegistryServer
yarn config set npmRegistryServer https://registry.example.com
yarn npm login
yarn npm audit

Settings live in .yarnrc.yml; .npmrc is ignored. Registries use npmRegistryServer, tokens use npmAuthToken.

See: Configuration

References

0 Comments for this cheatsheet. Write yours!