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

6

7

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
Raulrausch answered 31/10, 2022 at 10:50 Comment(2)
What does which yarn return? – Tarboosh
@Tarboosh for me it returns global yarn (.nvm/versions/node/...) – Mcbride
R
6

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.

Raulrausch answered 31/10, 2022 at 11:56 Comment(0)
F
18

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.

Fogbow answered 23/11, 2022 at 18:23 Comment(2)
Wow spent like half an hour on this banging my head against a wall. Running corepack [email protected] -v over and over again and continually seeing 3.6.1 – Translunar
You have to call yarn set version 4.0.1 it's super confusing since corepack use should matter. – Cardew
R
6

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.

Raulrausch answered 31/10, 2022 at 11:56 Comment(0)
C
2

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!

Cud answered 4/1, 2023 at 5:13 Comment(0)
T
1

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

Trygve answered 19/8, 2023 at 17:36 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Zirconia
this was similar to what happened to us but with pnpm there was a .pnpm-global at the home directory and removing it allowed corepack to work as expected. – Unhook
C
1

Based on yarn set version documentation we always have to call yarn set version 4.4.0.

Corepack is installing and replacing the yarn binary, but yarn itself is repointing itself to the last call of yarn set version XYZ or yarn set version self

Example

corepack enable

corepack use [email protected]
yarn -v # (4.3.1)

corepack use [email protected]
yarn -v # (4.3.1)...failure

yarn set version 4.4.1
yarn -v # (4.4.1) Success!

corepack use [email protected]
yarn -v # (4.4.1) Still got to do that set version call!

This problem happens with yarn 1.x.x projects too. Without a yarn-path you must call yarn set version 1.22.22 otherwise it uses yarn 4 to install on a packageManager of 1.22.22

I submitted a a yarn berry issue 6480 about clarifying why yarn set version x.y.z is superseding corepack use [email protected].

Cardew answered 2/9, 2024 at 8:7 Comment(0)
O
0

Tip 1: Check your projects for .yarnrc.yml

Look for .yarnrc.yml files in all project folders (including hidden ones) and home.

Tip 2: Look for yarnPath

Inside .yarnrc.yml, look for yarnPath:

yarnPath: .yarn/releaes/yarn-3.8.2.cjs

Now, comment out or remove yarnPath:

# yarnPath: .yarn/releaes/yarn-3.8.2.cjs

Now, try again. Optional: Consider starting a new terminal and cd'ing back where you were.

Explanation: You don't need yarnPath with corepack

If you're using corepack, you don't need to point to a yarnPath or host a .yarn/releases anymore.

Potential corepack bug? In my case, corepack is disregarding packageManager in package.json!

Tested with: yarn 3.8.2, corepack 0.28.0, node.js v20.13.0

Further troubleshooting

For the rest of the cases, this may be very niche to your environment and shell setup.

  • If you have a long-running terminal, start a fresh one so you have a clean baseline. Do this often, so your present directory and environment variables don't mask a root cause!

  • Verify Corepack is enabled:

    • No node.js version manager: cat $(which yarn)
    • For asdf users: cat $(asdf which yarn).
    • For nvm users: cat $(nvm which yarn).

    You should see something like:

    #!/usr/bin/env node
    process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='1'
    require('./lib/corepack.cjs').runMain(['yarn', 
    ...process.argv.slice(2)]);
    

    If not, run corepack enable.

    nvm/asdf users: This must be ran for each Node version.

  • If the problem persists, check your PATH and version managers (e.g. .nvmrc, .tool-version).

  • Check for any conflicts with version managers like nvm, asdf, or asdf-yarn.

If any of this helped, before you go too far - check your other projects to see if this applies as well.

P.S. This also intersects with an issue on yarnpkg/berry's tracker: Add a warning or error in Yarn Classic if package.json's packageManager key is set, but corepack is not enabled (berry#5912). While it's not an exact bulls-eye, there are many ways an environment or project can be misconfigured.

Outstay answered 8/9, 2024 at 13:25 Comment(0)

© 2022 - 2025 β€” McMap. All rights reserved.