Skip to content

Yarn

yarn is a JavaScript package manager that installs a project’s dependencies from the npm registry, records their exact versions in a yarn.lock file, and runs the scripts in package.json. Each project pins its own Yarn release, so every teammate and CI machine installs with the same version.

Facebook released Yarn in 2016. Yarn 2 and later, often called Yarn Berry, replaced the original 1.x line. That line is now known as Yarn Classic, and its code is frozen apart from occasional hotfixes. Other package managers for the same job include npm and pnpm.

Installation and Setup

Yarn’s docs install it through Corepack, a small tool that downloads the Yarn release a project asks for. Node.js 26 doesn’t ship Corepack, so install it from npm first and then turn on its yarn command:

Language: Shell
$ npm install -g corepack
$ corepack enable

Outside a project, this yarn command runs Yarn Classic. The init -2 command creates a new project that uses the latest modern release:

Language: Shell
$ mkdir shop
$ cd shop
$ yarn init -2
$ yarn --version
4.18.1

The command also writes a packageManager field such as "[email protected]" into package.json, which is how Corepack knows which release to run.

Key Features

  • Pins the Yarn release for each project through the packageManager field in package.json.
  • Installs with Plug’n’Play by default, which writes a .pnp.cjs loader instead of a node_modules folder.
  • Switches back to a classic node_modules folder with the nodeLinker: node-modules setting in .yarnrc.yml.
  • Manages monorepos through workspaces declared in the root package.json.
  • Runs a package’s command once, without adding it to the project, through yarn dlx.

Usage

Say a new shop project needs Prettier to format its code. The add command with -D installs Prettier and saves it under devDependencies:

Language: Shell
$ yarn add -D prettier
➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + prettier@npm:3.9.10
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0013: │ A package was added to the project (+ 9.5 MiB).
➤ YN0000: └ Completed in 0s 423ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 568ms

A script in package.json gives the formatter a short name:

Language: JSON
{
  "scripts": {
    "format": "prettier --write ."
  }
}

Yarn runs a script given only its name, so yarn format works as a shortcut for yarn run format:

Language: Shell
$ yarn format
cart.js 41ms
package.json 3ms (unchanged)
README.md 31ms (unchanged)

After cloning the project, a teammate runs yarn with no arguments to install every dependency at the versions in the lockfile:

Language: Shell
$ yarn

The up command upgrades a dependency across the whole project, including every workspace:

Language: Shell
$ yarn up prettier

The dlx command downloads a package into a temporary environment and runs its command once:

Language: Shell
$ yarn dlx create-vite my-app

For every command and setting, see the Yarn documentation.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 6, 2026