Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yarnpkg

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?

like image 378
designorant Avatar asked Jan 27 '20 15:01

designorant


People also ask

Is yarn 2 backwards compatible?

Backwards Compatibility with node_modules However, Yarn 2 now offers an option that copies packages to the node_modules/ folder just like Yarn 1, providing backward compatibility for these projects. It literally requires adding a single line to your new .

How do I install a specific version of yarn?

You can specify versions using one of these: yarn add package-name installs the “latest” version of the package. yarn add [email protected] installs a specific version of a package from the registry. yarn add package-name@tag installs a specific “tag” (e.g. beta , next , or latest ).

Can yarn and npm be used interchangeably?

Yarn can consume the same package. json format as npm, and can install any package from the npm registry. This will lay out your node_modules folder using Yarn's resolution algorithm that is compatible with the node.

How do I install all dependencies on yarn?

Installing all dependencies: yarn or yarn install. Installing one and only one version of a package: yarn install --flat. Forcing a re-download of all packages: yarn install --force. Installing only production dependencies: yarn install --production.


1 Answers

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.

like image 98
Maël Nison Avatar answered Sep 19 '22 02:09

Maël Nison