Yarn Package Manager

Yarn package manager for JavaScript/Node.js projects.


Installation

1# Via npm
2npm install -g yarn
3
4# Via Corepack (Node.js 16.10+)
5corepack enable
6corepack prepare yarn@stable --activate
7
8# Verify
9yarn --version

Basic Commands

 1# Initialize project
 2yarn init
 3yarn init -y  # Skip questions
 4
 5# Install dependencies
 6yarn install
 7yarn  # Shorthand
 8
 9# Add package
10yarn add package-name
11yarn add package-name@version
12yarn add package-name@tag
13
14# Add dev dependency
15yarn add -D package-name
16yarn add --dev package-name
17
18# Add peer dependency
19yarn add -P package-name
20
21# Add optional dependency
22yarn add -O package-name
23
24# Remove package
25yarn remove package-name
26
27# Upgrade package
28yarn upgrade package-name
29yarn upgrade package-name@version
30
31# Upgrade all packages
32yarn upgrade
33
34# Upgrade interactive
35yarn upgrade-interactive
36yarn upgrade-interactive --latest

Workspaces

Setup Monorepo

1// package.json
2{
3  "name": "my-monorepo",
4  "private": true,
5  "workspaces": [
6    "packages/*"
7  ]
8}

Workspace Commands

 1# Install all workspace dependencies
 2yarn install
 3
 4# Add dependency to specific workspace
 5yarn workspace package-a add lodash
 6
 7# Run script in workspace
 8yarn workspace package-a run build
 9
10# Run script in all workspaces
11yarn workspaces run build
12yarn workspaces run test
13
14# List workspaces
15yarn workspaces info
16
17# Run command in specific workspace
18yarn workspace package-a <command>

Scripts

 1// package.json
 2{
 3  "scripts": {
 4    "start": "node index.js",
 5    "dev": "nodemon index.js",
 6    "build": "webpack",
 7    "test": "jest",
 8    "lint": "eslint ."
 9  }
10}
1# Run script
2yarn run start
3yarn start  # Shorthand for some scripts
4
5# Run with arguments
6yarn test -- --coverage
7
8# List available scripts
9yarn run

Yarn Berry (v2+)

Enable Yarn Berry

1# Set version
2yarn set version berry
3yarn set version stable
4yarn set version 3.6.0
5
6# Check version
7yarn --version

Zero-Installs

 1# Enable PnP (Plug'n'Play)
 2yarn config set nodeLinker pnp
 3
 4# Commit .yarn directory
 5git add .yarn
 6git add .pnp.cjs
 7git commit -m "Enable Zero-Installs"
 8
 9# Install without node_modules
10yarn install

Plugins

 1# Add plugin
 2yarn plugin import interactive-tools
 3yarn plugin import workspace-tools
 4yarn plugin import version
 5
 6# List plugins
 7yarn plugin list
 8
 9# Remove plugin
10yarn plugin remove @yarnpkg/plugin-interactive-tools

Lock File

 1# Generate lock file
 2yarn install
 3
 4# Update lock file
 5yarn install --mode=update-lockfile
 6
 7# Check lock file
 8yarn install --immutable
 9yarn install --frozen-lockfile  # Yarn 1.x
10
11# Why is package installed?
12yarn why package-name

Cache

 1# Clear cache
 2yarn cache clean
 3
 4# Clear specific package
 5yarn cache clean package-name
 6
 7# Cache directory
 8yarn cache dir
 9
10# List cache
11yarn cache list

Configuration

 1# Set config
 2yarn config set registry https://registry.npmjs.org/
 3yarn config set nodeLinker node-modules
 4
 5# Get config
 6yarn config get registry
 7
 8# List config
 9yarn config list
10
11# Delete config
12yarn config unset registry

.yarnrc.yml (Yarn Berry)

 1# .yarnrc.yml
 2nodeLinker: node-modules
 3
 4npmRegistryServer: "https://registry.npmjs.org"
 5
 6yarnPath: .yarn/releases/yarn-3.6.0.cjs
 7
 8plugins:
 9  - path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
10    spec: "@yarnpkg/plugin-interactive-tools"

Security

 1# Audit dependencies
 2yarn audit
 3
 4# Audit and fix
 5yarn audit --fix  # Yarn 1.x
 6yarn npm audit --all --recursive  # Yarn Berry
 7
 8# Check licenses
 9yarn licenses list
10yarn licenses generate-disclaimer

Publishing

 1# Login
 2yarn login
 3
 4# Publish package
 5yarn publish
 6
 7# Publish with tag
 8yarn publish --tag beta
 9
10# Publish with new version
11yarn publish --new-version 1.0.1
12
13# Unpublish
14yarn unpublish package-name@version

Yarn vs npm

FeatureYarnnpm
Lock fileyarn.lockpackage-lock.json
Installyarnnpm install
Add packageyarn addnpm install
Removeyarn removenpm uninstall
Run scriptyarn <script>npm run <script>
Workspacesβœ…βœ…
PnPβœ… (v2+)❌

Troubleshooting

 1# Clear cache and reinstall
 2yarn cache clean
 3rm -rf node_modules
 4rm yarn.lock
 5yarn install
 6
 7# Check integrity
 8yarn install --check-files
 9
10# Verbose output
11yarn install --verbose
12
13# Network issues
14yarn install --network-timeout 100000
15
16# Offline install
17yarn install --offline

Docker Integration

 1# Dockerfile
 2FROM node:18-alpine
 3
 4WORKDIR /app
 5
 6# Copy package files
 7COPY package.json yarn.lock ./
 8
 9# Install dependencies
10RUN yarn install --frozen-lockfile --production
11
12# Copy source
13COPY . .
14
15# Build
16RUN yarn build
17
18CMD ["yarn", "start"]

CI/CD

 1# GitHub Actions
 2name: CI
 3
 4on: [push, pull_request]
 5
 6jobs:
 7  build:
 8    runs-on: ubuntu-latest
 9    
10    steps:
11      - uses: actions/checkout@v3
12      
13      - name: Setup Node
14        uses: actions/setup-node@v3
15        with:
16          node-version: '18'
17          cache: 'yarn'
18      
19      - name: Install dependencies
20        run: yarn install --frozen-lockfile
21      
22      - name: Run tests
23        run: yarn test
24      
25      - name: Build
26        run: yarn build

Create and Publish Package

Package Structure

1my-package/
2β”œβ”€β”€ src/
3β”‚   └── index.js
4β”œβ”€β”€ test/
5β”‚   └── index.test.js
6β”œβ”€β”€ package.json
7β”œβ”€β”€ README.md
8β”œβ”€β”€ LICENSE
9└── .yarnrc.yml  # For Yarn Berry

Initialize Package

1# Create package.json
2yarn init
3
4# Or interactive
5yarn init -y

package.json

 1{
 2  "name": "my-package",
 3  "version": "1.0.0",
 4  "description": "A useful package",
 5  "main": "dist/index.js",
 6  "types": "dist/index.d.ts",
 7  "scripts": {
 8    "build": "tsc",
 9    "test": "jest",
10    "prepublishOnly": "yarn build && yarn test"
11  },
12  "keywords": ["utility", "helper"],
13  "author": "Your Name <email@example.com>",
14  "license": "MIT",
15  "repository": {
16    "type": "git",
17    "url": "https://github.com/username/my-package.git"
18  },
19  "files": [
20    "dist",
21    "README.md",
22    "LICENSE"
23  ],
24  "dependencies": {},
25  "devDependencies": {
26    "typescript": "^5.0.0",
27    "jest": "^29.0.0"
28  },
29  "engines": {
30    "node": ">=16.0.0"
31  }
32}

Build and Test

 1# Build
 2yarn build
 3
 4# Test
 5yarn test
 6
 7# Check what will be published
 8yarn pack --dry-run
 9
10# Create tarball
11yarn pack

Publish to npm Registry

 1# Login to npm
 2yarn login
 3
 4# Publish package
 5yarn publish
 6
 7# Publish with tag
 8yarn publish --tag beta
 9
10# Publish with new version
11yarn publish --new-version 1.0.1
12
13# Publish scoped package (public)
14yarn publish --access public

Versioning

 1# Update version
 2yarn version --patch  # 1.0.0 -> 1.0.1
 3yarn version --minor  # 1.0.0 -> 1.1.0
 4yarn version --major  # 1.0.0 -> 2.0.0
 5
 6# Set specific version
 7yarn version --new-version 2.0.0
 8
 9# Pre-release
10yarn version --prepatch  # 1.0.0 -> 1.0.1-0
11yarn version --preminor  # 1.0.0 -> 1.1.0-0
12yarn version --premajor  # 1.0.0 -> 2.0.0-0

Private Registry

Using Verdaccio

 1# Install Verdaccio
 2npm install -g verdaccio
 3
 4# Run Verdaccio
 5verdaccio
 6
 7# Configure Yarn
 8yarn config set registry http://localhost:4873/
 9
10# Publish
11yarn publish --registry http://localhost:4873/

Using GitHub Packages

 1# Create .yarnrc.yml (Yarn Berry)
 2npmRegistries:
 3  "https://npm.pkg.github.com":
 4    npmAlwaysAuth: true
 5    npmAuthToken: "${GITHUB_TOKEN}"
 6
 7# Or .npmrc (Yarn Classic)
 8@myorg:registry=https://npm.pkg.github.com
 9//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
10
11# Publish
12yarn publish

Using Artifactory

1# Configure registry
2yarn config set registry https://artifactory.example.com/api/npm/npm-local/
3
4# Publish
5yarn publish --registry https://artifactory.example.com/api/npm/npm-local/

Yarn Workspaces Publishing

1# Publish all workspace packages
2yarn workspaces foreach --all publish
3
4# Publish specific workspace
5yarn workspace @myorg/package-a publish
6
7# Publish with version bump
8yarn workspaces foreach --all version patch
9yarn workspaces foreach --all publish

Related Snippets