94

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

tk421
  • 5,775
  • 6
  • 23
  • 34
me-me
  • 5,139
  • 13
  • 50
  • 91
  • Seems there are some wrinkles depending on your shell, though most answers appear not to address this detail. For oh-my-zsh, I found this answer: https://superuser.com/a/1663972/886184 – Kay V Apr 15 '22 at 13:10

6 Answers6

161

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.

tmt
  • 2,021
  • 1
  • 10
  • 17
  • Doesn't seem to work still puts yarn it old node version -->/Users/mpayne/.nvm/versions/node/v6.11.4/bin/yarn -> /Users/mpayne/.nvm/versions/node/v6.11.4/lib/node_modules/yarn/bin/yarn.js /Users/mpayne/.nvm/versions/node/v6.11.4/bin/yarnpkg -> /Users/mpayne/.nvm/versions/node/v6.11.4/lib/node_modules/yarn/bin/yarn.js + yarn@1.12.3 updated 1 package in 0.827s even though --> nvm current v8.12.0 – Matthew Payne Dec 17 '18 at 16:59
  • Why do you have node v6.11.4 in your results? Can you post "nvm list"? – tmt Jan 05 '19 at 00:42
81

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.

XPLOT1ON
  • 2,994
  • 2
  • 20
  • 36
  • I scoured the gh-nvm issues before asking this, but there are soooo many issues -- is there a way to update/install these default-packages without reinstalling all of node? – Cam Jul 15 '21 at 17:52
  • the way to update is like any other globally installed node library `npm update -g ` or without package_name to update all. Although this will update the currently active node environment. @Cam – XPLOT1ON Dec 30 '21 at 16:13
  • Thanks for the answer, this is really helpful! Kind of strange the "default-packages" isn't default behavior and instead requires additional configuration. – Brady Dowling Jan 21 '22 at 15:06
  • This solution doesn't work for [nvm-windows](https://github.com/coreybutler/nvm-windows). – abbr Mar 08 '23 at 22:51
  • @abbr `nvm-windows` is a completely different library compared to `nvm.sh` so `it doesn't work` is an expected behavior :) as of today – XPLOT1ON Mar 09 '23 at 11:34
  • In case anyone runs into the same problems, I noticed that this doesn't work correctly if the file only contains `yarn` and doesn't end with a newline – Jan Apr 05 '23 at 08:43
11

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

If so, delete the content in it.

Greenonline
  • 1,330
  • 8
  • 23
  • 31
CrayonApe
  • 383
  • 2
  • 12
  • 9
    this is not the way to do this, `~/.nmprc` files generally have other config settings in it for github access etc etc - DO NOT DELETE CONTENTS of this file – ggomersall Jul 04 '22 at 10:56
5

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
doubleya
  • 479
  • 9
  • 17
  • 6
    This will work but will also cause yarn installed with brew to be used no matter what node version is used with nvm. So if you ever want to use different yarn versions on different projects, this will cause problems. – halileohalilei Feb 27 '19 at 16:17
  • This "could" work on mac. But when you use NVM it's better to use the included NPM to install yarn. This allows you to have a yarn per NVM installed node version and is platform independant. The brew based command will fix it once; but it will break when you install or update a node version with NVM. – acidjunk Sep 16 '22 at 13:06
4

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.

2

I faced similar issue on mac wherein node v14.20.0 was installed via nvm.

In case of node installation via nvm, it creates symlink target as shown below.

/Users/<user_username>/.nvm/versions/node/v14.20.0/bin/corepack -> ../lib/node_modules/corepack/dist/corepack.js

In such cases, remove existing symlink by executing following coo

rm /Users/<user_username>/.nvm/versions/node/v14.20.0/bin/corepack

Now install yarn via if Node.js <16.10

npm i -g corepack

In case Node.js >=16.10

corepack enable
Gyro
  • 733
  • 7
  • 11