How can I install multiple versions of yarn

Viewed 814

I have several projects I'm participating in. Unfortunately they are using multiple versions of yarn. I'd like thus to have several versions installed and an easy (or even automated) way of switching between.

I had my hopes for a moment with nvm alias, thinking that maybe I'll get a second copy of node, with distinct global packages. I could then have e.g. nvm use v10.0-yarn1.3.4 or something like this, but nvm alias only creates a different name, the global packages directory stays the same.

1 Answers

Yes you can do that without changing node.js version in between.

  1. Install asdf - it is a package manager that helps to install and manage multiple vcersions of the same cli-program.

https://asdf-vm.com/#/core-manage-asdf?id=install

(if you have mac, install it using git as they recommend).

  1. https://github.com/twuni/asdf-yarn - run: asdf plugin-add yarn

  2. enjoy:

$ asdf install yarn latest # at the time of the writing, it will be 1.22.10
$ asdf global yarn 1.22.10
$ yarn
yarn install v1.22.10
[1/5]   Validating package.json...
[2/5]   Resolving packages...
success Already up-to-date.
✨  Done in 0.60s.
$ asdf install yarn 1.22.0
$ asdf global yarn 1.22.0
$ yarn
yarn install v1.22.0
[1/5]   Validating package.json...
[2/5]   Resolving packages...
success Already up-to-date.
✨  Done in 0.58s.

Notes:

  • You can also only change the yarn version in the current shell. Please have a look at https://asdf-vm.com/#/core-manage-versions?id=install-version to see how (I didn't test it yet).

  • Running npx yarn@1.22.1 install is not equal to yarn install when yarn version is 1.22.1 because npx will add some enviroment variables to your process which are npm-specific, while you are trying to use yarn. In some rare cases this may cause problems. Do what works best for you :)

Related