Node corepack is ignoring packageManager and won't let me set yarn version

4.6k Views Asked by At

I've cloned a project which uses Yarn 1.x and am trying to run it, but I can't find a way to get the right version of Yarn. I think I must be missing something.

Commented Terminal session:

# using node16.18.0
$ nvm use v16
Now using node v16.18.0 (npm v8.19.2)

# start with no yarn installed
$ yarn --version
zsh: command not found: yarn

# package.json has packageManager set for Yarn v1.22.19
$ cat package.json | grep packageManager
  "packageManager": "[email protected]"

# enable corepack, and it ignores the packageManager version
$ corepack enable
$ yarn --version
3.2.4

# manually ask corepack to use v1.22.19, but it again ignores this
$ corepack prepare [email protected] --activate
Preparing [email protected] for immediate activation...
$ yarn --version
3.2.4

# manually ask corepack to run yarn 1.22.19, but it again ignores it
$ corepack [email protected] --version
3.2.4

So using Corepack I don't seem to be able to convince it to use version of Yarn.

In addition installing using npm install -g also doesn't seem to work correctly.

# disable corepack so there's no yarn installed
$ corepack disable
$ yarn --version
zsh: command not found: yarn

# install yarn v1.22.19
$ npm install -g [email protected]

added 1 package, and audited 2 packages in 326ms

found 0 vulnerabilities

# somehow it's installed 3.2.4 again
$ yarn --version
3.2.4
4

There are 4 best solutions below

0
On BEST ANSWER

This doesn't address the corepack issue but I have been able to get npm install -g [email protected] to work. I found a file ~/.yarnrc.yml which had a version 3.2.4 in it. I'm not sure if the npm yarn version was actually incorrect, or if it just used ~/.yarnrc.yml as its source of truth incorrectly. But regardless when I deleted this file and reinstalled yarn globally at the desired version it worked correctly.

0
On

Ran into this as well. Thanks for the helpful pointers on the homepath. On my macos in a new terminal, I watched what was accessing the ~/.yarnrc.yml

sudo fs_usage | grep /Users/exampleuser/.yarn

In a second terminal, I ran

cd ~/exampleproject
yarn set version canary
yarn version 

This showed me that global telemetry on [email protected] was recreating the .yml in my homepath. I disabled it globally

yarn config set --home enableTelemetry 0

However, this was not enough to fix, as the watcher showed that node was still accessing that file even after everything was deleted, so I had to shutdown all terminals, VSCode, etc.

After taking the above steps, and restarting a new terminal, I was able to get the yarn version to respect the project settings.

cd ~/exampleproject
yarn set version canary
yarn --version // correct canary version
yarn install // profit

Further, re-enabling telemetry did not have the same effect.

cd ~/exampleproject
yarn config set --home enableTelemetry 1
yarn --version // still respects project version

I could only repro the problem via corepack

corepack enable
corepack prepare yarn@stable --activate
cd ~/example-project
yarn --version //-> showed corepack version instead of project version

which makes me think there's something lurking in there. Hope it helps!

1
On

I had a similar issue recently. But managed to fix it. (I don't know if it's the correct way of doing it but at least it worked for me)

which yarn would return ~/.nvm/versions/node/v18.12.1/bin/yarn and yarn --version return 3.2.4, and this even after doing the exact same thing as you. (trying to install and use yarn 1.22.19)

Then I figured I would check my home folder ~ and found there a .yarn folder and .yarnrc.yml file even though i supposedly uninstalled every global installation of yarn.

Removing this ~/.yarn folder and ~/.yarnrc.yml file fixed my issue and i was able to use yarn 1.22.19 as expected. corepack also stopped ignoring my commands

Hopefully that works for you as well.

2
On

remove .yarn folder and .yarnrc.yml file. it works!