Yarn Classic, Yarn Berry & Corepack
"Yarn" means two different tools. Yarn 1 ("classic", 2016) was a faster, lockfile-first alternative to npm 4, and is now frozen in maintenance mode at 1.22. Yarn 2+ ("Berry", 2020, now Yarn 4) is a rewrite with a different philosophy: Plug'n'Play instead of node_modules, a zip cache that can be committed, a plugin system and strict lockfile checks. The same yarn command can run either, depending on what the project asks for, and the piece that decides is Corepack.
Corepack: the package manager is a dependency too
A project that says "use pnpm" in its README but not in a machine-readable way gets installed with whatever pnpm version each developer happens to have. Corepack, shipped with Node, fixes that: the packageManager field in package.json names the exact tool and version, and Corepack's yarn/pnpm shims run exactly that version.
corepack --version
yarn --version
mkdir -p classic && cd classic
echo '{ "name": "classic", "version": "1.0.0", "packageManager": "yarn@1.22.22" }' > package.json
yarn --version
cd .. && mkdir -p modern && cd modern
echo '{ "name": "modern", "packageManager": "yarn@4.18.1" }' > package.json
yarn --version
echo '{ "name": "modern", "packageManager": "pnpm@10.34.7" }' > package.json
pnpm --version
COREPACK_ENABLE_STRICT=1 yarn install 2>&1 | head -10.36.0
4.18.1
1.22.22
4.18.1
10.34.7
This project is configured to use pnpm because /home/learner/modern/package.json has a "packageManager" field
The same yarn command ran 4.18.1 outside a project (the default this machine is set up with) and 1.22.22 in the project that asked for it. With strict mode on (Corepack's default; this container switches it off), using the wrong tool in a project is an error.
| Command | Effect |
|---|---|
corepack enable |
install the yarn/pnpm shims (once per machine) |
corepack use pnpm@10.34.7 |
write packageManager into package.json (with a hash) and install it |
corepack up |
move to the newest version within the current major |
corepack install -g yarn@4.18.1 |
pre-download for offline use (how this container was prepared) |
Note: Node 25+ no longer bundles Corepack; it becomes a separate package (
npm install -g corepack). ThepackageManagerfield stays the standard way to pin a project's tool, and pnpm and Yarn both read it themselves too.
Yarn classic in one session
Yarn 1 looks a lot like npm: a hoisted node_modules and its own lockfile format.
mkdir -p classic && cd classic
echo '{ "name": "classic", "version": "1.0.0", "license": "MIT", "packageManager": "yarn@1.22.22" }' > package.json
yarn add lodash@4.17.21 2>&1 | grep -v -E 'DEP0169|trace-deprecation'
cat yarn.lock
lsyarn add v1.22.22
warning You don't appear to have an internet connection. Try the --offline flag to use the cache for registry queries.
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ lodash@4.17.21
info All dependencies
└─ lodash@4.17.21
Done in 0.69s.
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
lodash@4.17.21:
version "4.17.21"
resolved "http://localhost:4873/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
node_modules
package.json
yarn.lock
The "internet connection" warning is Yarn 1's offline check, which pings the public registry. A yarn.lock v1 entry is keyed by the requested range (lodash@4.17.21, or lodash@^4.17.0), unlike npm's install paths. If you meet Yarn 1 in an existing project, it still works, but plan the move: it receives no new features, and its resolution of peer dependencies and workspaces predates npm 7.
Yarn Berry: the differences that matter
mkdir -p berry && cd berry
echo '{ "name": "berry", "packageManager": "yarn@4.18.1" }' > package.json
touch yarn.lock
yarn add lodash@4.17.21
ls -a
sed -n '1,20p' yarn.lock➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + lodash@npm:4.17.21
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0013: │ A package was added to the project (+ 1.49 MiB).
➤ YN0000: └ Completed in 0s 209ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 320ms
.
..
.pnp.cjs
.yarn
package.json
yarn.lock
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
__metadata:
version: 10
cacheKey: 10c0
"berry@workspace:.":
version: 0.0.0-use.local
resolution: "berry@workspace:."
dependencies:
lodash: "npm:4.17.21"
languageName: unknown
linkType: soft
"lodash@npm:4.17.21":
version: 4.17.21
resolution: "lodash@npm:4.17.21"
checksum: 10c0/d8cbea072bb08655bb4c989da418994b073a608dffa608b09ac04b43a791b12aeae7cd7ad919aa4c925f33b48490b5cfe6c1f71d827956071dae2e7bb3a6b74c
languageName: node
| Yarn 1 (classic) | Yarn 4 (Berry) | |
|---|---|---|
| Install layout | node_modules (hoisted) |
Plug'n'Play (.pnp.cjs), or node_modules / pnpm-style via nodeLinker |
| Packages stored as | unpacked folders | zip archives in a cache (global or .yarn/cache) |
| Lockfile | yarn.lock v1 |
yarn.lock in YAML with checksum, resolution, linkType |
| Config | .yarnrc, .npmrc |
.yarnrc.yml only |
| Dependency install scripts | run | off by default (enableScripts) |
| Workspaces | basic | workspace: protocol, foreach, constraints |
| CI mode | --frozen-lockfile |
--immutable (on automatically in CI) |
| Output | [1/4] Resolving packages... |
➤ YN0000: ┌ Resolution step (each YNxxxx is a documented message code) |
yarn add lodash@4.17.21 saved the version exactly as you typed it; yarn add lodash would save ^4.18.1. The output codes are handy for search: YN0013 means "fetched packages", YN0085 lists resolution changes.
Note: In this course's container,
yarn initcan't run (it tries to write to Corepack's read-only shared cache), so sessions createpackage.jsonby hand plus an emptyyarn.lock. The emptyyarn.locktells Yarn "this folder is a project root", so it doesn't go looking for a parent project.
The next lessons cover what makes Berry different: Plug'n'Play, workspaces and constraints.
Test yourself
What does the packageManager field in package.json do?
- Installs dependencies
- Pins the exact package manager and version, which Corepack's shims then run
- Chooses the registry
- Marks the package as a CLI
Which statement about Yarn 1 (classic) is true?
- It uses Plug'n'Play by default
- It is in maintenance mode and uses a hoisted node_modules
- It reads .yarnrc.yml
- It disables install scripts by default
What is Yarn Berry's equivalent of npm ci's refusal to change the lockfile?
- yarn install --frozen-lockfile
- yarn install --immutable
- yarn ci
- yarn install --pure