Yarn 16 ๐งถ Cheatsheet
1. Toolchain & Corepack
corepack enable # Enable Corepack (Node.js 16.10+)
yarn set version stable # Use latest Yarn Berry (4.x)
yarn set version classic # Downgrade to Yarn 1.x
yarn set version 4.5.0 # Pin exact Yarn version
yarn --version # Check active Yarn version
2. Project Initialization
yarn init # Create public package.json
yarn init -p # Create private package.json
yarn init -w # Create workspace root (monorepo)
yarn init -i=latest # Initialize with version lock
3. Dependency Management
yarn add <pkg> # Add to dependencies
yarn add -D <pkg> # Add to devDependencies
yarn add -P <pkg> # Add to peerDependencies
yarn add -O <pkg> # Add to optionalDependencies
yarn add <pkg>@<version> # Add exact version
yarn remove <pkg> # Remove a dependency
yarn up <pkg> # Upgrade package to latest (Berry)
yarn up --interactive # Choose upgrades interactively
yarn up '*' # Upgrade all packages
4. Installation & Lockfile
yarn install # Install dependencies
yarn install --immutable # CI: fail if lockfile changes
yarn install --immutable-cache # CI: fail if cache changes (Zero-Installs)
yarn install --check-cache # CI: verify package checksums
yarn install --mode=skip-build # Skip postinstall scripts
5. Running Scripts & Binaries
yarn <script> # Run package.json script (no 'run' needed)
yarn run <script> # Explicit run command
yarn run <bin> # Run binary from dependencies
yarn run --binaries-only <bin># Skip scripts, force binary
yarn dlx <pkg> # Run package without installing (npx equivalent)
6. Workspaces (Monorepos)
yarn workspaces foreach -Apt run <script> # Run script across all workspaces (-A=all, -p=parallel, -t=topological)
yarn workspaces foreach --since=origin/main run test # Only changed workspaces
yarn workspaces focus <name> # Install only dependencies for one workspace
yarn workspaces focus -A --production # Focus all, omit devDependencies
7. Configuration (.yarnrc.yml)
nodeLinker: pnp # pnp (default), node-modules, or pnpm
enableGlobalCache: false # false = .yarn/cache (Zero-Installs), true = ~/.yarn
yarnPath: .yarn/releases/yarn-4.5.0.cjs # Pin local binary
npmRegistryServer: "https://registry.npmjs.org"
npmAuthToken: "${NPM_TOKEN}" # Inject secret via env var
npmScopes:
my-org:
npmRegistryServer: "https://npm.internal.com"
npmAuthToken: "${INTERNAL_TOKEN}"
8. Plug’n’Play (PnP)
yarn dlx @yarnpkg/pnpify --sdk vscode # Generate IDE SDK for VS Code
yarn config set nodeLinker node-modules # Fallback if PnP breaks tools
9. Zero-Installs
# .yarnrc.yml
enableGlobalCache: false
nodeLinker: pnp
# .gitignore
.yarn/*
!.yarn/cache
!.pnp.cjs
!.pnp.loader.mjs
git add .yarn/cache .pnp.cjs yarn.lock
10. Patching Dependencies
yarn patch <pkg> # Extract package to temp folder
yarn patch <pkg> --update # Extract with existing patch applied
yarn patch-commit -s <folder> # Save patch and update package.json
11. Publishing Packages
yarn npm publish # Publish current workspace
yarn npm publish --dry-run # Preview tarball without publishing
yarn npm publish --access public # Public scoped package
yarn npm publish --tag beta # Attach dist-tag
yarn npm publish --provenance # CI only: link to source repo
12. Cache & Registry Management
yarn cache clean # Clear cache
yarn config get cacheFolder # Get path to global/local cache
yarn npm info <pkg> # View package registry metadata
13. Environment Variables
CI=true # Automatically enables immutable installs
YARN_ENABLE_IMMUTABLE_INSTALLS=0 # Override immutable for a single command
YARN_ENABLE_HARDENED_MODE=1 # Enable hardened mode for non-PR workflows
14. CI/CD GitHub Actions (Standard)
- uses: actions/checkout@v4
- name: Enable Corepack
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
15. npm to Yarn Migration
# To Yarn Classic:
yarn import # Convert package-lock.json to yarn.lock
# To Yarn Berry:
corepack enable
yarn set version berry
rm package-lock.json # Delete npm lockfile
# Translate .npmrc to .yarnrc.yml (Berry ignores .npmrc)
yarn install
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!