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
| Command | Action / Purpose |
|---|---|
yarn install | Install all dependencies declared in package.json |
yarn add lodash | Add lodash dependency to current package |
yarn add -D typescript | Add typescript as devDependency |
yarn remove express | Uninstall package dependency |
yarn up react | Upgrade react across all workspace packages to latest |
yarn dlx create-next-app | Execute 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! SetnodeLinker: node-modulesif third-party libraries break.
[!TIP] Use
yarn dlxinstead ofnpxwhen running one-off executable package scripts in Yarn v4 projects.