Yarn not installing in nvm version node version

NpmYarnpkgNvm

Npm Problem Overview


I'm running into an issue with yarn when I change my nvm version of node. I noticed when I check my ~/.nvm folder I see two node versions.

  • v8.11.0
  • v8.11.3.

I installed yarn globally. using npm install -g yarn when I was using v8.11.0.

I can see yarn in my

.nvm/versions/node/v8.11.0

But when I switch to nvm v8.11.3 or set my nvm alias default to v8.11.3
Yarn is no longer available. I tried doing a global install again hoping it would add it to my v8.11.3 folder but it keeps trying to add it to v8.11.0

I've even deleted folder v8.11.0 but it just recreates it when I run npm install -g yarn

How can I get it to install so I can use yarn using any node version switch in nvm

Npm Solutions


Solution 1 - Npm

When you install a new node version using nvm and then used npm to install yarn, you need to reinstall the yarn for the new node version.

Try:

nvm install 8.11.3
nvm use 8.11.3
npm install -g yarn

This will install yarn in:

.nvm/versions/node/v8.11.3/

You can then switch between 8.11.0 and 8.11.3 and your yarn will still work.

Solution 2 - Npm

The problem that OP described caused by the fact that globally installed packages lives within their respected namespace (their version), and it cannot be shared across versions. There are a few ways around this. The NON-RECOMMEND WAY is to install yarn via brew, apt or non-node package manager. Although it works, but things may break.

The RECOMMEND WAY is described below.
nvm has a very nice default packages installer. This will installed specified packages when installing a new node version using nvm.

create a text file at $NVM_DIR/default-packages, usually it is located at ~/.nvm/default-packages, with a list of npm packages to be installed. The content may looks like the following

@vue/cli
create-react-app
firebase-tools
yarn

Documentation link here

try running nvm install --lts to install node's latest lts version, packages specified in the default-packages will be installed automatically.

Solution 3 - Npm

Check to see if there is a ~/.npmrc file.

If so, delete the content in it.

Solution 4 - Npm

I recently ran into this issue (on a mac). I had to use

brew install yarn --ignore-dependencies

and that did it for me. Yarn is available no matter what node version I switch to with nvm. Hopefully this helps someone. More information can be found here: https://yarnpkg.com/lang/en/docs/install/#mac-stable

Solution 5 - Npm

Following the installation guide on official documentation:

If using nvm you can avoid the node installation by doing:

sudo apt update && sudo apt install --no-install-recommends yarn

Note: Due to the use of nodejs instead of node name in some distros, yarn might complain about node not being installed. A workaround for this is to add an alias in your .bashrc file, like so: alias node=nodejs. This will point yarn to whatever version of node you decide to use.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionme-meView Question on Stackoverflow
Solution 1 - NpmTMTView Answer on Stackoverflow
Solution 2 - NpmXPLOT1ONView Answer on Stackoverflow
Solution 3 - NpmbellemereView Answer on Stackoverflow
Solution 4 - NpmdoubleyaView Answer on Stackoverflow
Solution 5 - NpmOrest BorovetsView Answer on Stackoverflow