Yarn User Manual

Basic Command Comparison

Yarn 4+ (Modern Version) vs Yarn Classic (v1)

FeatureYarn 4+Yarn Classic
Initialize Projectyarn init -2yarn init
Add Dependencyyarn add packageyarn add package
Install Dependenciesyarn installyarn or yarn install
Remove Dependencyyarn remove packageyarn remove package
Upgrade Dependencyyarn up packageyarn upgrade package

Yarn 4+ Advanced Features

Workspaces

Workspaces are the name of individual packages that are part of the same project and that Yarn will install and link together to simplify cross-references.

Declaring Workspaces

Add a workspaces array to the root package.json:

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

Workspace Cross-References

Use the workspace: protocol to establish dependencies between workspaces:

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

Focused Installs

Install only the dependencies required for specific workspaces:

# Install only @my-org/app and its dependencies
yarn workspaces focus @my-org/app

# Install only production dependencies
yarn workspaces focus -A --production

Parallel Execution

Run scripts in parallel across multiple workspaces:

# Publish all packages in parallel (respecting topological order)
yarn workspaces foreach --all -pt npm publish

# Run lint only on modified workspaces
yarn workspaces foreach --since run lint

# Run build on current workspace and its dependencies
yarn workspaces foreach --from . -R run build

Constraints

Constraints are used to declare rules that must apply to specific workspaces in your project, similar to ESLint for code:

  • Enforce synchronized dependency versions across the project
  • Prevent certain dependencies from being used
  • Enforce proper settings for fields like license, engines.node, etc.

Global Scripts

Script names containing colons (:) are treated as global scripts and can be run from anywhere within the project.

Yarn Classic Basic Usage

Common Commands

# Initialize new project
yarn init

# Add dependency
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

# Add to different dependency categories
yarn add [package] --dev      # Development dependencies
yarn add [package] --peer     # Peer dependencies  
yarn add [package] --optional # Optional dependencies

# Upgrade dependency
yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

# Remove dependency
yarn remove [package]

# Install all dependencies
yarn
# or
yarn install

Version Management

Yarn 4+ Version Switching

# Switch to stable version
yarn set version stable

# Switch to Canary version
yarn set version canary

# Build from sources
yarn set version from sources

Yarn Classic Version Management

Yarn Classic is typically managed through npm:

# Upgrade to latest version
npm install --global yarn@latest

Best Practices

New Projects Recommendations

  • Use Yarn 4+ to get the latest features and performance improvements
  • Fully leverage workspace functionality to manage complex project structures
  • Use constraints to ensure project consistency

Existing Project Migration

  • Evaluate compatibility of existing dependencies and workflows
  • Gradually migrate to modern version, testing first on non-critical projects
  • Leverage Yarn's migration tools and documentation

Performance Optimization

  • Yarn 4+ provides better performance through parallel installs and offline caching
  • Focused installs in workspaces reduce unnecessary dependency downloads
  • Leverage plugin system to extend functionality without increasing core complexity

Note: This document is based on official content from yarnpkg.com and classic.yarnpkg.com websites, including detailed usage methods and best practices for each version.