>_cat yarn.md|Web & App Servers
Yarn Cheatsheet
SPEC: MDX·/yarn·HTTP reverse proxies, application runtimes, and log analysis.
Yarn is a package manager that doubles as project management tool. It handles dependencies with speed, reliability, and security, supporting both classic workflows and modern multi-package monorepos.
Installation
Modern Yarn (v2+ / Berry via Corepack - Recommended)
Corepack is bundled with Node.js 16.10+:
corepack enable
corepack prepare yarn@stable --activate
# Verify version
yarn -vYarn Classic (v1.x - Legacy)
npm install --global yarnUpgrading Yarn Berry in Project
yarn set version stableProject Initialization & Configuration
yarn init # Interactive project setup
yarn init -2 # Initialize with Yarn Berry defaults
yarn config get npmRegistryServer # Check current registry URL
yarn config set npmRegistryServer https://registry.npmjs.orgManaging Dependencies
Installing
yarn install # Install all dependencies (or simply `yarn`)
yarn install --immutable # Strict CI installation (fails if lockfile changes)
yarn install --check-cache # Verify integrity of cacheAdding Packages
yarn add package-name # Add dependency to dependencies
yarn add -D package-name # Add dependency to devDependencies
yarn add package-name@^18.0.0 # Add specific version / tag
yarn add -P package-name # Add peer dependency
yarn global add package-name # Global install (Yarn Classic)Removing & Updating
yarn remove package-name # Remove dependency
yarn up package-name # Upgrade package across workspaces (Berry)
yarn upgrade package-name # Upgrade package (Classic)
yarn upgrade-interactive # Interactive dependency update consoleInspection & Cleaning
yarn why package-name # Explain why package is in dependency tree
yarn info package-name # Print package metadata from registry
yarn cache clean # Clear global cache
yarn dedupe # Deduplicate matching dependencies (Berry)Running Scripts & Binaries
yarn <script> # Run custom script defined in package.json
yarn run build # Explicit script execution
yarn dlx package-name # Run one-off package without installing (like npx)Workspaces (Monorepo Management)
Yarn workspaces allow managing multiple sub-packages inside a single Git repository.
Defining Workspaces in Root package.json
{
"private": true,
"workspaces": [
"apps/*",
"packages/*"
]
}Workspace Commands
# List all registered workspaces
yarn workspaces list
# Execute command inside a specific workspace
yarn workspace @repo/web dev
yarn workspace @repo/web add axios
# Run command across all workspaces in parallel
yarn workspaces run build
yarn workspaces run testQuick Reference
| Command | Description |
|---|---|
yarn | Install dependencies |
yarn add pkg | Add production package |
yarn add -D pkg | Add development package |
yarn dlx pkg | Execute binary one-off (npx equivalent) |
yarn workspace <name> <cmd> | Execute command in specific monorepo workspace |
yarn dedupe | Clean duplicate versions in lockfile |