Yarn Package Manager
Yarn is a fast, reliable, and secure alternative to npm for managing JavaScript dependencies.
This doc is a practical reference to Yarn CLI commands grouped by purpose.
Project Initialization
shell
# Create a new package.json file interactively
yarn init
# Create a package.json with default values (no prompts)
yarn init -yInstalling Dependencies
shell
# Install all dependencies listed in package.json
yarn install
# Add a new runtime dependency
yarn add lodash
# Add a specific version
yarn add [email protected]
# Add a package from a GitHub repository (the master branch) to the current workspace using a URL
yarn add lodash@https://github.com/lodash/lodash
# Add as a development-only dependency
yarn add typescript -D
yarn add typescript --dev
# Add as a peer dependency
yarn add react -P
yarn add react --peer
# Add as an optional dependency
yarn add fsevents -O
yarn add fsevents --optional
# Add a global package
yarn global add typescriptRemoving & Upgrading Dependencies
shell
# Remove a dependency
yarn remove lodash
# Remove a global package
yarn global remove typescript
# Upgrade a dependency to the latest version respecting semver
yarn upgrade lodash
# Interactive upgrade menu (choose what to upgrade)
yarn upgrade-interactive
# Interactive upgrade to latest versions, ignoring semver
yarn upgrade-interactive --latestRunning Scripts
shell
# Run a custom script from package.json (e.g., "build", "test", etc.)
yarn run build
# Shortcut for yarn run start
yarn start
# Shortcut for yarn run test
yarn testManaging Dependencies
shell
# List all installed packages
yarn list
# Show why a specific package was installed (dependency tree)
yarn why react
# Show which dependencies are outdated
yarn outdatedCache & Cleanup
shell
# List all cached packages
yarn cache list
# Clean the cache
yarn cache clean
# Run a clean script (if defined in package.json)
yarn cleanWorkspaces (Monorepos)
shell
# Show workspace dependency graph
yarn workspaces info
# Run a command in a specific workspace
yarn workspace my-package buildConfiguration
shell
# Get a config value
yarn config get registry
# Set a config value
yarn config set registry https://registry.npmjs.org/Miscellaneous
shell
# Set Yarn version for the project (Yarn 2+ feature)
yarn set version canary
# CI Safe Install: Ensures the yarn.lock file is not changed – great for CI environments
yarn install --frozen-lockfile