Skip to content

/commands/yarn/yarn-set-version

yarn set version stable: change the Yarn version used by a project

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Yarn
versions
Yarn 2+, Yarn 4, Yarn 1.22
os
any

yarn set version stable pins the current project to the latest stable Yarn release; yarn set version 4.5.3 pins an exact version and yarn set version classic goes back to Yarn 1. Yarn records the version in the packageManager field of package.json and, when Corepack is not enabled, also downloads the release into .yarn/releases/ and points yarnPath in .yarnrc.yml at it, so the global yarn command forwards to that file inside the project.

Pin a version

bash
echo '{ "name": "app", "version": "1.0.0", "private": true }' > package.json
npx --yes @yarnpkg/cli-dist@4 set version 4.5.3 2>&1 | grep -E "Corepack|Saving"
npm pkg get packageManager
cat .yarnrc.yml
node .yarn/releases/yarn-4.5.3.cjs --version

Output

➤ YN0000: You don't seem to have Corepack enabled; we'll have to rely on yarnPath instead
➤ YN0000: Saving the new release in .yarn/releases/yarn-4.5.3.cjs
"yarn@4.5.3"
yarnPath: .yarn/releases/yarn-4.5.3.cjs
4.5.3

Corepack is not enabled in this environment, so Yarn fell back to yarnPath: it saved the release as .yarn/releases/yarn-4.5.3.cjs and wrote yarnPath to .yarnrc.yml. The packageManager field is written either way. The example starts Yarn through npx @yarnpkg/cli-dist@4; with Yarn installed the command is yarn set version 4.5.3.

Version specifiers

CommandResult
yarn set version stableLatest stable Yarn release
yarn set version canaryLatest canary (release candidate) build
yarn set version 4.5.3An exact version
yarn set version 4.xNewest release matching the range (Yarn 2+ releases only)
yarn set version classicLatest Yarn 1.22 release
yarn set version ./yarn.cjsA local Yarn bundle
yarn set version --only-if-needed stableLock the version only if none is locked yet
yarn set version stable --yarn-pathAlso download the release and set yarnPath, even with Corepack available

Yarn and Corepack

Corepack ships with Node.js up to version 24; Node.js 25 and later no longer bundle it, and it is installed separately with npm install -g corepack. After corepack enable, the yarn command reads packageManager from package.json and downloads and runs exactly that version, so no .yarn/releases file is needed. With Corepack, corepack use yarn@4.5.3 is the equivalent command: it updates packageManager (with a hash) and runs an install.

Pitfalls

  • Global Yarn 1 still installed. The global yarn 1.22 binary respects yarnPath, which is why the per-project release works without Corepack. Much older Yarn 1 versions do not; update the global one to 1.22.
  • Mismatch errors. When Corepack is enabled and packageManager names a different tool or version than the one you run, Corepack refuses and says the project is configured to use another package manager. Run the command through the right tool, or change the field with corepack use.
  • New lockfile format. Moving from Yarn 1 to Yarn 2+ rewrites yarn.lock on the next yarn install; commit it in the same change.
  • yarn: command not found means no Yarn is on PATH at all; see yarn: command not found for the Corepack and npm ways to get one.

How to confirm it worked

Run yarn --version inside the project: it prints the pinned version, while the same command outside the project prints the global one. npm pkg get packageManager shows what is recorded for everybody else.

FAQ

How do I upgrade a project from Yarn 1 to Yarn 4?

Run yarn set version stable in the project (Yarn 1.22 forwards the command to a modern Yarn), then yarn install to convert the lockfile. Add nodeLinker node-modules to .yarnrc.yml if your tools are not ready for Plug'n'Play, and commit the changes.

Should I commit .yarn/releases?

If the project uses yarnPath, yes: everyone, including CI, then runs exactly that Yarn file. If the team uses Corepack, the packageManager field is enough and the release file is not needed.

How do I go back to Yarn 1?

yarn set version classic sets the project to the latest Yarn 1.22 release. Remove the .yarnrc.yml settings that only Yarn 2+ understands, and delete .pnp.cjs if it exists.

Sources