Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn not installing in nvm version node version

Tags:

npm

nvm

yarnpkg

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

like image 730
me-me Avatar asked Sep 04 '18 21:09

me-me


People also ask

Do I need to install yarn for every Node version?

The Yarn maintainers recommend installing Yarn globally by using the NPM package manager, which is included by default with all Node.

Can NVM install yarn?

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. You can then switch between 8.11. 0 and 8.11. 3 and your yarn will still work.

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 ).


2 Answers

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.

like image 198
TMT Avatar answered Sep 28 '22 13:09

TMT


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.

like image 21
XPLOT1ON Avatar answered Sep 28 '22 11:09

XPLOT1ON