corepack icon indicating copy to clipboard operation
corepack copied to clipboard

What does it mean to enable Yarn 3.x "globally"?

Open pastelmind opened this issue 2 years ago • 5 comments

Hi. This is a question, not a bug report, so feel free to move the discussion elsewhere as needed.

As far as I know, the only version of Yarn meant to be installed globally is the "legacy" v1.x line. Versions 2 and beyond (berry) are meant to be installed separately for each repository under the .yarn/releases/ subdirectory.

However, when I run corepack prepare --activate yarn@stable, it appears to activate Yarn 3.x globally.

$ corepack prepare --activate yarn@stable
Preparing yarn@stable for immediate activation...
$ yarn --version
3.5.0

Note that the above was executed outside any project directory, so there is no "local copy" anywhere.

Am I missing something? Or does this mean that Yarn Berry can be installed globally (i.e. no local copy needed) in conjunction with Corepack?

Edit: This has practical implications. For example, if I "globally" install Yarn 3.x via corepack prepare, the yarn global add command is no longer usable since it was removed in Berry. So it does affect some workflows that rely on globally installed packages (whether this is an acceptable practice is another matter).

pastelmind avatar Mar 18 '23 05:03 pastelmind

Or does this mean that Yarn Berry can be installed globally (i.e. no local copy needed) in conjunction with Corepack?

It can, but it's not recommended in the Yarn install guide. In some cases it's still acceptable though (for example with Docker images where you only have to deal with a single modern project).

arcanis avatar Mar 18 '23 07:03 arcanis

Thanks. In which case I feel we should not recommend updating Yarn via corepack prepare --activate yarn@stable as it would install Yarn Berry globally. Would you recommend an alternate method for keeping the legacy, global version of Yarn up to date?

pastelmind avatar Mar 18 '23 08:03 pastelmind

You can pin the install to 1.x:

corepack prepare --activate [email protected]

arcanis avatar Mar 18 '23 08:03 arcanis

When you follow the official installation steps yarn 3 gets installed globally. Took me a while to realise that corepack installed version 3 globally. Mainly because it's part of the official installation instructions.

corepack prepare yarn@stable --activate

https://yarnpkg.com/getting-started/install#updating-the-global-yarn-version

nphmuller avatar Apr 25 '23 14:04 nphmuller

Also created an issue in the yarn website repository: https://github.com/yarnpkg/berry/issues/5410

nphmuller avatar Apr 25 '23 14:04 nphmuller

I think the question was answered, and the Yarn project has since updated its documentation AFAICT. Closing for now, let me know if I missed something.

aduh95 avatar Feb 21 '24 05:02 aduh95