'How to install Yarn v1 and Yarn v2 on the same machine so they can be used interchangelby between projects?

I'm running a couple of projects that make use of Yarn v1 that will not be upgraded to Yarn v2 anytime soon for many reasons (CI being one of them). However, I would like to use Yarn v2 for new projects.

Is there a sane way to have both Yarn v1 and Yarn v2 installed on one machine so they can be used interchangebly between projects?



Solution 1:[1]

We recommend using Yarn 1 as global so as to avoid possible backward compatibility issues (for example later versions renamed some options and settings). You then have two options:

  • You can use yarn policies set-version berry on a per-project basis; it will install the very latest Yarn releases in the .yarn/releases folder, which you can then add to your repository. Docs for yarn policies.

  • Or you can add a "packageManager": "[email protected]" field to your package.json file and run corepack enable. This will use the Corepack tool to setup your system in such a way that any yarn commands will use the exact version you configured for the active project.

This keeps the risks to a minimum and allows you to migrate when you feel ready.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1