What it is
Yarn is a fast, reliable, and secure JavaScript package manager for managing your project’s dependencies. You reach for Yarn when you need to install, update, or remove packages for your Node.js projects.
Installation
Linux (Debian/Ubuntu):
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
Linux (Fedora/CentOS/RHEL):
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum install yarn # or dnf install yarn
macOS: Using Homebrew:
brew install yarn
Windows: Download the installer from the official Yarn website: https://classic.yarnpkg.com/en/docs/install
Core Concepts
package.json: The manifest file for your project. It describes your project, lists its dependencies, and includes scripts. Yarn reads and writes to this file.yarn.lock: A file generated by Yarn that locks down the exact versions of every package and sub-dependency that were installed. This ensures reproducible builds across different environments. Do not edit this file manually.- Dependencies: Packages that your project needs to run. These are listed in
package.jsonunderdependencies(for production) anddevDependencies(for development). - Registry: By default, Yarn fetches packages from the official npm registry. You can configure it to use other registries.
Commands / Usage
Project Initialization and Setup
yarn init: Initializes a new Node.js project. Prompts you for basic information and creates apackage.jsonfile.yarn inityarn init -y: Initializes a new Node.js project with default settings, skipping the prompts.yarn init -yyarn install: Installs all dependencies listed inpackage.jsonandyarn.lock. Ifyarn.lockexists, it installs the exact versions specified. If not, it resolves dependencies and createsyarn.lock.yarn installyarn install --frozen-lockfile: Installs dependencies exactly as specified inyarn.lock, failing ifpackage.jsonandyarn.lockare out of sync. Useful for CI environments.yarn install --frozen-lockfileyarn install --production: Installs only production dependencies (ignoresdevDependencies).yarn install --production
Adding and Removing Dependencies
yarn add <package-name>: Adds a package to your project’s dependencies and updatespackage.jsonandyarn.lock. Installs the latest version by default.yarn add lodashyarn add <package-name> --dev: Adds a package as a development dependency (todevDependenciesinpackage.json).yarn add jest --devyarn add <package-name>@<version>: Adds a specific version of a package.yarn add react@17.0.2yarn add <package-name>@^<version>: Adds a package with a caret semantic version range (e.g.,^17.0.2allows17.x.xbut not18.0.0).yarn add express@^4.17.1yarn add <package-name>@~<version>: Adds a package with a tilde semantic version range (e.g.,~17.0.2allows17.0.xbut not17.1.0).yarn add axios@~0.21.1yarn add <package-name> --exact: Adds a package with an exact version number, no range.yarn add typescript --exactyarn remove <package-name>: Removes a package from your project, updatingpackage.jsonandyarn.lock.yarn remove lodashyarn remove <package-name> --dev: Removes a development dependency.yarn remove jest --dev
Running Scripts and Commands
yarn <script-name>: Executes a script defined in thescriptssection of yourpackage.json.# Assuming "test": "jest" is in package.json yarn testyarn <command> <args...>: Executes a binary from a package’snode_modules/.bindirectory.yarn eslint src/index.jsyarn dlx <package-name> [args...]: Executes a package’s binary without installing it globally or locally first. It downloads the package, runs the command, and then cleans up. Similar tonpx.yarn dlx create-react-app my-app
Updating Dependencies
yarn upgrade: Updates all packages to the latest versions allowed bypackage.jsonandyarn.lock.yarn upgradeyarn upgrade <package-name>: Updates a specific package to the latest version allowed bypackage.jsonandyarn.lock.yarn upgrade lodashyarn upgrade <package-name> --latest: Updates a specific package to its absolute latest version, ignoringpackage.jsonversion constraints. This will updatepackage.jsonandyarn.lock.yarn upgrade lodash --latestyarn upgrade --latest: Updates all packages to their absolute latest versions, ignoringpackage.jsonversion constraints. This will updatepackage.jsonandyarn.lock.yarn upgrade --latest
Package Management and Information
yarn outdated: Lists packages that have newer versions available than what’s currently installed.yarn outdatedyarn info <package-name>: Displays information about a package from the registry.yarn info reactyarn list: Lists all the packages installed in your project, including their dependencies.yarn listyarn why <package-name>: Shows why a particular package is installed (which of your dependencies requires it).yarn why react
Cleaning and Caching
yarn cache clean: Clears the Yarn cache. This forces Yarn to re-download packages the next time they are needed.yarn cache cleanyarn cache list: Lists packages in the Yarn cache.yarn cache listyarn clean: Removes thenode_modulesfolder and theyarn.lockfile.yarn clean
Publishing Packages
yarn publish: Publishes your package to the npm registry. Requires you to be logged in (yarn login) and have apublishConfigsection in yourpackage.jsonif you’re publishing to a scoped registry.yarn publishyarn publish --new-version <version>: Publishes a new version of your package.yarn publish --new-version 1.2.3yarn login: Logs you into a package registry (e.g., npm).yarn loginyarn logout: Logs you out of a package registry.yarn logout
Workspaces (Monorepos)
yarn workspaces list: Lists all workspaces in your monorepo.yarn workspaces listyarn workspace <workspace-name> <command>: Runs a command within a specific workspace.yarn workspace @my-scope/ui add reactyarn workspaces foreach <command>: Runs a command in each workspace.yarn workspaces foreach run buildyarn workspaces foreach --tag <tag> <command>: Runs a command in each workspace that has a specific tag.yarn workspaces foreach --tag frontend run build
Common Patterns
- Install project dependencies:
yarn install - Add a production dependency:
yarn add express - Add a development dependency:
yarn add typescript --dev - Run a script defined in
package.json:yarn start yarn build - Run a linter on specific files:
yarn eslint src/ - Update all packages to their latest allowed versions:
yarn upgrade - Update a specific package to its absolute latest version:
yarn upgrade react --latest - Check for outdated packages:
yarn outdated - Clean install (remove
node_modulesandyarn.lock, then reinstall):yarn cache clean && rm -rf node_modules yarn.lock && yarn install - Install dependencies in a CI environment (ensuring lockfile is used):
yarn install --frozen-lockfile - Install only production dependencies:
yarn install --production
Gotchas
yarn.lockis crucial: Always commit youryarn.lockfile to version control. It guarantees that everyone on your team and your CI/CD pipeline installs the exact same dependency versions, preventing "it works on my machine" issues.- Global vs. Local Installation: Yarn primarily manages local project dependencies. Avoid using
yarn global addunless absolutely necessary, as it can lead to version conflicts and make projects less portable. Prefer usingyarn dlxor adding dev dependencies topackage.jsonfor command-line tools. yarn installvs.yarn upgrade:yarn installprioritizes theyarn.lockfile. If it exists, it installs exactly what’s in the lock file.yarn upgraderespects the version ranges inpackage.jsonand updatesyarn.lockaccordingly.--frozen-lockfilein CI: This flag is essential for CI/CD pipelines. It ensures thatyarn installwill fail ifpackage.jsonandyarn.lockare not in sync, preventing unexpected dependency changes.- Cache Issues: If you encounter strange installation errors,
yarn cache cleancan sometimes resolve them by forcing a fresh download of packages. - Workspaces and
yarn.lock: In a monorepo using workspaces, Yarn typically hoists dependencies to the rootnode_modulesfolder to save space and avoid duplication. This means theyarn.lockfile at the root is the primary source of truth for all dependencies across all workspaces.