Yarn
Package Manager
Yarn
Overview
Yarn is a fast, secure, and reliable package manager for JavaScript/TypeScript developed as an alternative to npm. Co-developed by Meta (formerly Facebook), Google, Exponent, and Tilde, it provides advanced features like deterministic installs, offline capabilities, and workspaces while maintaining complete compatibility with npm. The current Yarn Berry (v2+) is a next-generation package manager that solves traditional node_modules issues through pluggable architecture and Plug'n'Play (PnP).
Details
Yarn was developed in 2016 to address npm's speed and consistency issues. The latest Yarn Berry (v3/v4) adopts a Plug'n'Play (PnP) approach that doesn't use traditional node_modules directories, achieving reduced disk usage and faster performance. The Workspaces feature simplifies managing multiple packages in monorepos, making dependency sharing and version management more efficient. Zero-Installs functionality allows projects to be runnable immediately after git clone by committing the .yarn directory. The Constraints feature enables defining dependency relationship rules between workspaces using Prolog-like syntax, maintaining consistency in large-scale projects.
Pros and Cons
Pros
- Fast installation: High-speed package installation through parallel processing and caching
- Deterministic installs: Reproducible build environments with yarn.lock files
- Workspaces feature: Efficient multi-package management in monorepos
- PnP (Plug'n'Play): Fast and lightweight package resolution without node_modules
- Zero-Installs: Commit dependencies to repository for installation-free workflows
- Plugin system: Rich functionality extension through plugins
Cons
- High learning curve: Need to understand concepts different from npm, especially PnP
- Ecosystem compatibility: Some packages may not be compatible with PnP
- Configuration complexity: Complex setup required for advanced features
- Migration cost: Time-consuming migration from existing npm projects
Reference Links
- Yarn Official Site
- Yarn Documentation
- Yarn GitHub
- Plug'n'Play Documentation
- Workspaces Feature
- Yarn Constraints
Code Examples
Basic Project Initialization
# Initialize a new project
yarn init
# Install packages
yarn install
# Add packages
yarn add react react-dom
# Add development dependencies
yarn add -D typescript @types/react
# Add global packages
yarn global add nodemon
Workspaces (Monorepo) Setup
// package.json
{
"name": "my-monorepo",
"private": true,
"workspaces": [
"packages/*"
]
}
# Run commands across all workspaces
yarn workspaces foreach run build
# Run commands in specific workspace
yarn workspace @myorg/app run dev
# Run commands only in changed workspaces
yarn workspaces foreach --since run test
# Add dependency between workspaces
yarn workspace @myorg/app add @myorg/utils@workspace:^
Script Execution and Task Management
# Execute package scripts
yarn run build
yarn run test
yarn run dev
# Execute binaries
yarn exec webpack
yarn bin webpack # Get binary path
yarn node --inspect $(yarn bin jest) # Execute with Node.js flags
# Display dependency information
yarn info react
yarn why lodash
yarn licenses list
Package Management and Version Control
# Update packages
yarn up react
yarn up -i # Interactive update
yarn upgrade-interactive
# Remove packages
yarn remove lodash
yarn remove -A lodash # Remove from all workspaces
# Cache management
yarn cache clean
yarn cache clean --all
Advanced Configuration and PnP
# .yarnrc.yml
nodeLinker: pnp # or node-modules
pnpMode: strict
# Workspaces hoisting limits
nmHoistingLimits: workspaces
# Add plugins
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"
Production Environment Optimization
# Production build (exclude devDependencies)
yarn install --production
yarn workspaces focus --production
# Zero-installs setup for Yarn 2+
# Add to .gitignore:
# .yarn/cache
# .yarn/unplugged
# .yarn/build-state.yml
# .yarn/install-state.gz
# Create package bundles
yarn pack
yarn workspaces foreach pack