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 inityarn init -y — Create package.json with default values.
yarn init -yyarn install — Install all dependencies from package.json.
yarn installyarn — Shorthand for yarn install.
yarnyarn install --frozen-lockfile — Install without updating yarn.lock (CI-safe).
yarn install --frozen-lockfileAdd & Remove Packages
yarn add <package> — Add a package as a dependency.
yarn add expressyarn add <package>@<version> — Add a specific version.
yarn add react@18.2.0yarn add -D <package> — Add as a dev dependency.
yarn add -D typescript eslintyarn add --peer <package> — Add as a peer dependency.
yarn add --peer reactyarn remove <package> — Remove a package.
yarn remove lodashyarn add <package> --exact — Add with exact version (no ^ or ~ prefix).
yarn add react --exactUpgrade & Info
yarn upgrade <package> — Upgrade a package to the latest within range.
yarn upgrade expressyarn upgrade --latest — Upgrade all packages to their latest versions.
yarn upgrade --latestyarn outdated — Show outdated packages.
yarn outdatedyarn info <package> — Show detailed information about a package.
yarn info reactyarn list — List installed packages.
yarn list --depth=0yarn why <package> — Show why a package is installed (dependency chain).
yarn why webpackScripts & Run
yarn run <script> — Run a script defined in package.json.
yarn run buildyarn <script> — Shorthand for yarn run.
yarn buildyarn start — Run the start script.
yarn startyarn test — Run the test script.
yarn testyarn dlx <package> — Execute a package without installing (like npx).
yarn dlx create-react-app my-appWorkspaces
yarn workspaces list — List all workspaces in the project.
yarn workspaces listyarn workspace <name> add <package> — Add a package to a specific workspace.
yarn workspace @myapp/server add expressyarn workspace <name> run <script> — Run a script in a specific workspace.
yarn workspace @myapp/client run buildyarn workspaces foreach run <script> — Run a script in all workspaces.
yarn workspaces foreach run testCache & Config
yarn cache clean — Clear the global package cache.
yarn cache cleanyarn config set <key> <value> — Set a configuration value.
yarn config set registry https://registry.npmjs.orgyarn config list — Show all configuration values.
yarn config listyarn 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
- yarnpkg.com – official project site with documentation and CLI reference
- Yarn – GitHub project – source code, releases and issues