纱线未安装在 nvm 版本节点版本

在更改 nvm 版本的节点时,遇到了纱线问题。 当我检查 ~/. nvm 文件夹时,我注意到有两个节点版本。

  • V8.11.0
  • V8.11.3.

我全局安装纱线。使用 Npm 安装-g 纱线当我使用 v8.11.0。

我可以看到我的毛线

.nvm/versions/node/v8.11.0

但是当我切换到 nvm v8.11.3或者将 nvm 别名默认设置为 v8.11.3时
纱线不再可用。我再次尝试进行全局安装,希望它能将其添加到我的 v8.11.3文件夹,但它一直试图将其添加到 v8.11.0

我甚至删除了文件夹 v8.11.0,但它只是重新创建它当我运行 npm 安装-g 纱

我如何让它安装,以便我可以使用纱线使用任何节点版本开关在 nvm

83353 次浏览

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.

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

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

If so, delete the content in it.

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.

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.

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