Fixing "packageManager: yarn@4.x" projects when Yarn 1.x is shadowing Corepack (macOS/Homebrew)
This is the "Yarn 4 project, but my shell keeps running Yarn 1.22" problem.
You'll see an error like:
This project's package.json defines "packageManager": "yarn@4.9.4". However the current global version of Yarn is 1.22.22. ...the project is meant to be used with Corepack...
- The project explicitly requires Yarn 4.9.4 via
packageManagerinpackage.json. - Node 22 includes Corepack, which can provide the correct Yarn version.
- But my machine still ran Yarn 1.22.22 because a global Yarn binary was earlier in
PATHand shadowed Corepack's Yarn shim.
Once I removed the global Yarn, yarn started resolving to Corepack-managed Yarn 4.9.4.
Running yarn dev:
- complains that the project wants Yarn 4.9.4
- says current Yarn is 1.22.22
- recommends enabling Corepack
Even after:
corepack enable
corepack prepare yarn@4.9.4 --activate...yarn -v still shows 1.22.22.
That means your yarn command is not Corepack's.
node -v
corepack --versioncorepack enable
corepack prepare yarn@4.9.4 --activatewhich -a yarn
type -a yarnExample output showed multiple yarn locations:
/opt/homebrew/opt/node@22/bin/yarn/opt/homebrew/bin/yarn
If yarn -v still prints 1.22.22 at this point, you're hitting a global Yarn install (often npm).
Homebrew uninstall may fail if Yarn isn't installed as a brew formula:
brew uninstall yarn
# Error: No such keg: /opt/homebrew/Cellar/yarnIf Yarn was installed via npm globally, remove it:
npm -g rm yarnhash -r
which yarn
yarn -vAt this point, yarn -v should print:
4.9.4
If it does, you're good.
Optional cleanup (helps when switching Yarn major versions):
rm -rf node_modules
rm -f yarn.lockThen install + run:
yarn install
yarn devIf you don't want to fight PATH yet, you can test via Corepack directly:
corepack yarn -v
corepack yarn devIf that works but yarn dev doesn't, the problem is definitely "wrong yarn binary in PATH."
- Yarn may print "A new stable version is available..." (e.g. 4.12.0). Ignore unless you want to upgrade the repo.
- If
which yarnpoints somewhere weird, you still have a shadowing install. - If
yarnis in multiple places,which -a/type -ais the fastest way to see what's winning.
corepack enable
corepack prepare yarn@4.9.4 --activate
which -a yarn
type -a yarn
npm -g rm yarn # if yarn is shadowing corepack
hash -r
yarn -v # should be 4.9.4
yarn install
yarn dev