简体   繁体   English

通过 npm 安装后找不到 yarn 命令

[英]yarn command not found after installing via npm

As per the yarn installation for yarn v2, they want you to install using npm install -g yarn .根据 yarn v2 的纱线安装,他们希望您使用npm install -g yarn So I ran sudo npm install -g yarn on Ubuntu 20.04.所以我在 Ubuntu 20.04 上运行了sudo npm install -g yarn But after I do that, it says command not found.但是在我这样做之后,它说找不到命令。

❯ sudo npm install -g yarn

> yarn@1.22.10 preinstall /usr/local/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)


❯ yarn --version
zsh: command not found: yarn
sudo npm install -g npm

then然后

sudo npm install -g yarn

Then reboot your system.然后重新启动系统。 That did it for me.那是为我做的。 Before a reboot only sudo yarn worked.在重新启动之前,只有 sudo yarn 有效。 I tried looking at file permissions but everything seemed in order and the files were executable as expected.我尝试查看文件权限,但一切似乎都井井有条,并且文件可以按预期执行。 Nevertheless after a reboot it worked.尽管如此,重新启动后它仍然有效。

If you go to /usr/local/bin after the installation there's a link there to where yarn.js lives, as expected, and file permissions for it were also correct.如果您在安装后 go 到 /usr/local/bin ,那么这里有一个指向 yarn.js 所在位置的链接,正如预期的那样,并且它的文件权限也是正确的。

/usr/local/bin is added to $PATH, so it's surprising that it doesn't see the new cmd right away, but perhaps it didn't reload or map it until after the reboot? /usr/local/bin 被添加到 $PATH,所以令人惊讶的是它没有立即看到新的 cmd,但也许它没有重新加载或 map 直到重新启动之后? I don't know.我不知道。 But I just spent a good hour trying to figure this out so I'm posting what worked for me to spare other the hassle.但我只是花了一个小时试图弄清楚这一点,所以我发布了对我有用的东西,以免其他人麻烦。

This solved it for me:这为我解决了:

corepack enable

(if you get "Internal Error: EACCES: permission denied", run it with sudo) (如果出现“内部错误:EACCES:权限被拒绝”,请使用 sudo 运行它)

This is also recommended by the Yarn documentation: https://yarnpkg.com/getting-started/install这也是 Yarn 文档推荐的: https://yarnpkg.com/getting-started/install

If you want to avoid reboot, use /usr/local/lib/node_modules/yarn/bin/yarn --version如果要避免重新启动,请使用/usr/local/lib/node_modules/yarn/bin/yarn --version

Uninstall cmdtest :卸载cmdtest

sudo apt remove cmdtest

Then, run these commands:然后,运行以下命令:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

TL;DR If you are managing node via nvm , then probably the path to yarn binary is not included in the $PATH variable. TL;DR如果您通过nvm管理节点,那么$PATH变量中可能不包含 yarn 二进制文件的路径。 You should add this -你应该添加这个 -

# Add this at the end (or after the $NVM_DIR initialization)
#   in your profile - .bashrc | .zshrc | .profile, etc
export PATH="`yarn global bin`:$PATH"

at the end of your profile file ( .zshrc for me) or at least after the $NVM_DIR initialization.在您的配置文件的末尾(对我来说是 .zshrc)或至少在$NVM_DIR .zshrc之后。


I have recently faced this issue and while searching for a solution, I landed up here.我最近遇到了这个问题,在寻找解决方案时,我来到了这里。

Here is what my environment looks like:这是我的环境的样子:

  • OS: Ubuntu 20.04操作系统:Ubuntu 20.04
  • Shell: zsh Shell: zsh
  • NodeJS: managing it via nvm , and NOT apt . NodeJS:通过nvm管理它,而不是apt

After going through all the answers, I was not keen on uninstalling anything.在浏览完所有答案后,我并不热衷于卸载任何东西。 So I tried to dig a bit deeper.所以我试图更深入地挖掘。

I installed yarn via npm install -g yarn command.我通过npm install -g yarn命令安装了yarn So the first thing I wanted to verify was the location of the yarn binary.所以我想验证的第一件事是yarn二进制文件的位置。 To do this, I ran the command where yarn which lists the installation path for the yarn binary.为此,我运行了where yarn命令,其中列出了yarn二进制文件的安装路径。

$ where yarn

/home/<user_name>/.nvm/versions/node/v16.11.1/bin/yarn

Then it hit me.然后它击中了我。 In my .zshrc file, I had added the yarn global bin command (which spills out the directory of all the global packages installed by yarn) at the top like so:在我的.zshrc文件中,我在顶部添加了yarn global bin命令(它会溢出由 yarn 安装的所有全局包的目录),如下所示:

# Top of my .zshrc file
export PATH="`yarn global bin`:$HOME/bin:/usr/local/bin:$PATH"

and as per the installation instruction of nvm , the $NVM_DIR (the variable which holds the nvm directory path) was added at the end of my .zshrc file.并根据nvm安装说明,在我的 .zshrc 文件末尾添加了$NVM_DIR .zshrc保存nvm目录路径的变量)。

So when I was starting up my shell, it was actually trying to load the yarn command (present inside the nvm directory) even before loading the $NVM_DIR path.因此,当我启动 shell 时,它实际上是在加载$NVM_DIR路径之前尝试加载yarn命令(存在于nvm目录中)。

To solve this, I tweaked my .zshrc file and moved the yarn global bin command after the $NVM_DIR like this:为了解决这个问题,我调整了我的.zshrc文件并将yarn global bin命令移动到$NVM_DIR之后,如下所示:

# Top of my .zshrc file
export PATH="$HOME/bin:/usr/local/bin:$PATH"

# ...
#
# Something in between
#
# ...

# Bottom of my .zshrc file
export NVM_DIR="${HOME}/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Here is where I have added the path to yarn global
export PATH="`yarn global bin`:$$PATH"

I hope that this would be of help.我希望这会有所帮助。

The yarn documentation is missing a step, you need to restart your computer between this installation and running yarn --version . yarn 文档缺少一个步骤,您需要在此安装和运行yarn --version之间重新启动计算机。

This worked for me这对我有用

I recently had a similar situation and here is how I solved it.我最近遇到了类似的情况,这就是我解决它的方法。

First I troubleshoot the current npm installation:首先我对当前的 npm 安装进行故障排除:

npm config -list

I had a ~/.npmrc file that had a different prefix:我有一个具有不同前缀的~/.npmrc文件:

PREFIX=/opt/homebrew

That made my npm installation look for globally installed packages under /opt/homebrew .这使我的 npm 安装在/opt/homebrew下寻找全局安装的包。

In my case, I'm using a different npm installation (not with homebrew anymore).就我而言,我使用的是不同的 npm 安装(不再使用自制软件)。 A simple fix is to remove this custom PREFIX from the ~/.npmrc file and the problem was solved.一个简单的修复是从~/.npmrc文件中删除这个自定义PREFIX ,问题就解决了。

Now npm looks for globally installed packages under /usr/local/bin/ .现在 npm 在/usr/local/bin/下寻找全局安装的包。

I installed yarn with npm install -g yarn on git bash and I tested it with yarn -v that show the version of the installed yarn, but when I used yarn start it gives me this error我用npm install -g yarn yarn git bash 上安装了 yarn,我用yarn -v测试了它,显示了安装的 yarn 的版本,但是当我使用yarn start ,它给了我这个错误

C:\Users\{username}\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found

These are simple steps that I used to fix my problem on Windows 10 :这些是我用来解决Windows 10问题的简单步骤:

  1. Uninstall node.js卸载 node.js
  2. Restart your computer重启你的电脑
  3. Delete your C:\Program Files\nodejs and C:\Users\{username}\AppData\Roaming\npm删除你的C:\Program Files\nodejsC:\Users\{username}\AppData\Roaming\npm
  4. Install node.js again and check it with node -v再次安装node.js,用node -v查看
  5. Start your vs code as an admin and write npm install以管理员身份启动你的 vs 代码并编写npm install
  6. Write yarn startyarn start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM