Yarn

Essential Yarn commands for package management in JavaScript and Node.js projects.

cli
yarnpackage-managernodejsjavascript

Installation

# Install Yarn globally via npm
npm install -g yarn

# Check Yarn version
yarn --version

Project Initialization

# Create a new package.json
yarn init

# Create package.json with defaults
yarn init -y

Installing Dependencies

# Install all dependencies from package.json
yarn install

# Install a package and add to dependencies
yarn add package-name

# Install a specific version
yarn add package-name@1.2.3

# Install a package as dev dependency
yarn add package-name --dev

# Install a package globally
yarn global add package-name

# Install exact version (no ^ or ~)
yarn add package-name --exact

Removing Dependencies

# Remove a package
yarn remove package-name

# Remove a global package
yarn global remove package-name

Updating Dependencies

# Update all dependencies
yarn upgrade

# Update a specific package
yarn upgrade package-name

# Update to latest version (ignoring version range)
yarn upgrade package-name --latest

# Interactive upgrade
yarn upgrade-interactive

Running Scripts

# Run a script defined in package.json
yarn run script-name

# Shorthand for running scripts
yarn script-name

# Run start script
yarn start

# Run test script
yarn test

# Run build script
yarn build

Dependency Information

# List installed packages
yarn list

# List top-level packages only
yarn list --depth=0

# Check why a package is installed
yarn why package-name

# Show information about a package
yarn info package-name

# Check for outdated packages
yarn outdated

Cache Management

# List cached packages
yarn cache list

# Clean the cache
yarn cache clean

# Get cache directory
yarn cache dir

Workspaces (Monorepo)

# Run command in all workspaces
yarn workspaces run build

# Add dependency to specific workspace
yarn workspace workspace-name add package-name

# Run script in specific workspace
yarn workspace workspace-name run script-name

Lock File

# Generate yarn.lock without installing
yarn install --frozen-lockfile

# Install without generating lock file
yarn install --no-lockfile

# Update the lock file
yarn install --force

Configuration

# Set a config value
yarn config set key value

# Get a config value
yarn config get key

# List all config
yarn config list

# Delete a config value
yarn config delete key

# Set registry
yarn config set registry https://registry.npmjs.org

Publishing

# Login to npm registry
yarn login

# Publish a package
yarn publish

# Publish with a new version
yarn publish --new-version 1.0.0

# Logout from registry
yarn logout

Utility Commands

# Check for issues
yarn check

# Create a compressed tarball
yarn pack

# Run a command with package binaries available
yarn run env

# Generate license report
yarn licenses list

# Link a local package
yarn link

# Unlink a local package
yarn unlink

Yarn 2+ (Berry) Specific

# Set Yarn version for project
yarn set version berry

# Enable Plug'n'Play
yarn config set nodeLinker pnp

# Disable Plug'n'Play (use node_modules)
yarn config set nodeLinker node-modules

# Install with immutable lock file (CI)
yarn install --immutable

# Add TypeScript plugin
yarn plugin import typescript