>_cat yarn.md|Web & App Servers
Index

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

Corepack is bundled with Node.js 16.10+:

corepack enable
corepack prepare yarn@stable --activate
 
# Verify version
yarn -v

Yarn Classic (v1.x - Legacy)

npm install --global yarn

Upgrading Yarn Berry in Project

yarn set version stable

Project 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.org

Managing 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 cache

Adding 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 console

Inspection & 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 test

Quick Reference

CommandDescription
yarnInstall dependencies
yarn add pkgAdd production package
yarn add -D pkgAdd development package
yarn dlx pkgExecute binary one-off (npx equivalent)
yarn workspace <name> <cmd>Execute command in specific monorepo workspace
yarn dedupeClean duplicate versions in lockfile