Yarn v4 Berry Cheat Sheet

Quick reference guide for Yarn v4 Berry: Workspaces syntax, Plug'n'Play (PnP) commands, constraints, and plugins.

Package Managers
yarn
package-manager
monorepo

Yarn v4 (Berry) is a modern, fast, and secure JavaScript package manager supporting Plug'n'Play (PnP) and native Monorepo Workspaces.

Configuration File (`.yarnrc.yml`)

yaml
nodeLinker: node-modules # Use node_modules or pnp
yarnPath: .yarn/releases/yarn-4.3.0.cjs

# Scoped NPM Registry settings
npmScopes:
  mycompany:
    npmRegistryServer: "https://npm.pkg.github.com"
    npmAuthToken: "${NPM_TOKEN}"

Essential `yarn` CLI Commands

Table
CommandAction / Purpose
yarn installInstall all dependencies declared in package.json
yarn add lodashAdd lodash dependency to current package
yarn add -D typescriptAdd typescript as devDependency
yarn remove expressUninstall package dependency
yarn up reactUpgrade react across all workspace packages to latest
yarn dlx create-next-appExecute one-off CLI tool package without installing globally

Monorepo Workspaces Syntax

bash
# Run command in specific workspace package
yarn workspace @app/web run build

# Run script in ALL workspace packages concurrently
yarn workspaces foreach --all --parallel run lint

# Add dependency to specific workspace package
yarn workspace @app/api add prisma

Common Pitfalls & Tips

[!WARNING] When using nodeLinker: pnp (Plug'n'Play), packages cannot read undeclared transitive dependencies! Set nodeLinker: node-modules if third-party libraries break.

[!TIP] Use yarn dlx instead of npx when running one-off executable package scripts in Yarn v4 projects.