Yarn — Package and Project Manager for Node.js

Practical guide to Yarn — fast JavaScript package manager and npm alternative with workspaces, offline caching and deterministic installs via lockfile.

Yarn is a fast JavaScript package manager and a well-established alternative to npm. Through its yarn.lock file, Yarn keeps your dependencies reproducible and installs them quickly thanks to an offline cache. With workspaces you can manage monorepos made up of several packages in a single project. It is worth knowing the difference between Yarn Classic (v1) and modern "Berry" (v2+), which introduces a different installation model via Plug'n'Play – so some commands differ between the two lines.

Install & Init

yarn init — Create a new package.json interactively.

yarn init

yarn init -y — Create package.json with default values.

yarn init -y

yarn install — Install all dependencies from package.json.

yarn install

yarn — Shorthand for yarn install.

yarn

yarn install --frozen-lockfile — Install without updating yarn.lock (CI-safe).

yarn install --frozen-lockfile

Add & Remove Packages

yarn add <package> — Add a package as a dependency.

yarn add express

yarn add <package>@<version> — Add a specific version.

yarn add react@18.2.0

yarn add -D <package> — Add as a dev dependency.

yarn add -D typescript eslint

yarn add --peer <package> — Add as a peer dependency.

yarn add --peer react

yarn remove <package> — Remove a package.

yarn remove lodash

yarn add <package> --exact — Add with exact version (no ^ or ~ prefix).

yarn add react --exact

Upgrade & Info

yarn upgrade <package> — Upgrade a package to the latest within range.

yarn upgrade express

yarn upgrade --latest — Upgrade all packages to their latest versions.

yarn upgrade --latest

yarn outdated — Show outdated packages.

yarn outdated

yarn info <package> — Show detailed information about a package.

yarn info react

yarn list — List installed packages.

yarn list --depth=0

yarn why <package> — Show why a package is installed (dependency chain).

yarn why webpack

Scripts & Run

yarn run <script> — Run a script defined in package.json.

yarn run build

yarn <script> — Shorthand for yarn run.

yarn build

yarn start — Run the start script.

yarn start

yarn test — Run the test script.

yarn test

yarn dlx <package> — Execute a package without installing (like npx).

yarn dlx create-react-app my-app

Workspaces

yarn workspaces list — List all workspaces in the project.

yarn workspaces list

yarn workspace <name> add <package> — Add a package to a specific workspace.

yarn workspace @myapp/server add express

yarn workspace <name> run <script> — Run a script in a specific workspace.

yarn workspace @myapp/client run build

yarn workspaces foreach run <script> — Run a script in all workspaces.

yarn workspaces foreach run test

Cache & Config

yarn cache clean — Clear the global package cache.

yarn cache clean

yarn config set <key> <value> — Set a configuration value.

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

yarn config list — Show all configuration values.

yarn config list

yarn global add <package> — Install a package globally (Yarn Classic).

yarn global add serve

Conclusion

Yarn combines fast, reproducible installs, an offline cache and first-class workspace support in a single tool – ideal for monorepos and larger projects. Pin versions deliberately (--exact) and use yarn install --frozen-lockfile (Classic) or yarn install --immutable (Berry) in CI pipelines so builds stay reproducible and the yarn.lock is not changed unnoticed. Keep an eye on your dependencies' install scripts – they are a common supply-chain vector. And remember not to mix Classic and Berry commands: what works under v1 is often named differently under v2+.

Further Reading

  • apk – package manager for Alpine Linux
  • apt – package management for Debian and Ubuntu
  • apt-get – classic package tool for Debian and Ubuntu