node.js 安装 Yeoman 后如何修复“yo: command not found”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21211340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:42:05  来源:igfitidea点击:

How to fix "yo: command not found" after installing Yeoman

macosnode.jsnpmosx-mountain-lionyeoman

提问by Joanna Marsden

Following these instructions, I tried to install yeoman using npm twice: http://yeoman.io/learning/index.html

按照这些说明,我尝试使用 npm 安装 yeoman 两次:http: //yeoman.io/learning/index.html

After the first failure, I uninstalled node using these instructions: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Then, I installed nvm and node/npm (via nvm) with one error:

第一次失败后,我使用以下说明卸载了 node: 如何完全卸载 Node.js,并从头重新安装 (Mac OS X)然后,我安装了 nvm 和 node/npm(通过 nvm),但出现了一个错误:

[Yeoman Doctor] Uh oh, I found potential errors on your machine
---------------

[Error] NPM root value is not in your NODE_PATH
  [info]
    NODE_PATH = /Users/joanna/.nvm/v0.10.22/lib/node_modules:.
    NPM root  = /Users/joanna/.node/lib/node_modules

  [Fix] Append the NPM root value to your NODE_PATH variable
    Add this line to your .bashrc
      export NODE_PATH=$NODE_PATH:/Users/joanna/.node/lib/node_modules
    Or run this command
      echo "export NODE_PATH=$NODE_PATH:/Users/joanna/.node/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

I pasted that command in, and then I ran npm install -g yoagain.

我粘贴了那个命令,然后我又跑npm install -g yo了。

After following the instructions from the yeoman site again, it still can't find yeoman. I receive this error: -bash: yo: command not found

再次按照 yeoman 站点的说明进行操作后,仍然找不到 yeoman。我收到此错误:-bash: yo: command not found

What is wrong? The Yeoman Doctor says: "Everything looks alright!"

怎么了?约曼医生说:“一切看起来都很好!”

采纳答案by Thijs Koerselman

The source of the problem is that you have NODE_PATH set while using NVM at the same time. When you use NVM you shouldn't have a NODE_PATH variable at all, since NVM installs global packages in its version specific root.

问题的根源在于您在使用 NVM 的同时设置了 NODE_PATH。当您使用 NVM 时,您根本不应该有 NODE_PATH 变量,因为 NVM 在其版本特定的根目录中安装全局包。

Delete all packages that are in your NODE_PATH, remove the environment variable, and install yo and the generators or any other global packages you need. You will see that it stops complaining and everything ends up in the nvm subdirectory matching the current version.

删除 NODE_PATH 中的所有包,删除环境变量,然后安装 yo 和生成器或您需要的任何其他全局包。您将看到它停止抱怨并且所有内容都在与当前版本匹配的 nvm 子目录中结束。

When you start using a different Node version in NVM, reinstall the global packages. This will keep everything organised neatly by version like its meant to be.

当您开始在 NVM 中使用不同的 Node 版本时,请重新安装全局包。这将使所有内容按版本整齐地组织起来,就像它本来的样子。

More info on this discussion here: https://github.com/creationix/nvm/pull/97

关于这里讨论的更多信息:https: //github.com/creationix/nvm/pull/97

回答by fregante

All I needed to do was addingthis line to .bash_profile

我需要做的就是将这一行添加.bash_profile

export PATH="$PATH":~/.node/bin


You can executethis to add it automatically:

您可以执行此操作以自动添加它:

printf "\nexport PATH=\"$PATH\":%s\n" ~/.node/bin >> ~/.bash_profile

Tested on OS X 10.9, 10.10, 10.11 and Ubuntu 14.04

在 OS X 10.9、10.10、10.11 和 Ubuntu 14.04 上测试

回答by Oleg Belousov

Short version:

精简版:

In your shell, type the following commands:

在您的 shell 中,键入以下命令:

  1. npm config set prefix ~/npm

  2. echo "export NODE_PATH=$NODE_PATH:/home/$USER/npm/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

  3. Then reinstall Yeoman:

    npm install -g yo

  1. npm config set prefix ~/npm

  2. echo "export NODE_PATH=$NODE_PATH:/home/$USER/npm/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

  3. 然后重新安装 Yeoman:

    npm install -g yo

Then everything should work fine!

然后一切正常!

Explanation:

解释:

Had a similar condition, except yo command did work, but any installed generators didn't appear after installing them, this solved the issue.

有类似的情况,除了 yo 命令确实有效,但安装后没有出现任何已安装的生成器,这解决了问题。

The problem is that your npm path is set to /usr/local, which is a directory that requires root/sudo privileges, since yo is a user command, it shouldn't be ran as a superuser, and if you'll try to run sudo yo, Yeoman will tell you that explicitly.

问题是你的 npm 路径设置为 /usr/local,这是一个需要 root/sudo 权限的目录,因为 yo 是一个用户命令,它不应该作为超级用户运行,如果你尝试run sudo yo,Yeoman 会明确地告诉你。

I also tried to chown -R $USER:$USER /usr/local, and chmod -R /user/local +rw, but none of those helped.

我也试过chown -R $USER:$USER /usr/local, 和chmod -R /user/local +rw,但这些都没有帮助。

Keep in mind that the node modules(yo generators are also node modules) that were previously installed in /usr/local might no longer be available and will require re-intalling.

请记住,之前安装在 /usr/local 中的节点模块(yo 生成器也是节点模块)可能不再可用,需要重新安装。

回答by svnm

For me on Ubuntu only the following worked for me...

对于我在 Ubuntu 上只有以下对我有用......

Ubuntu

Ubuntu

Tested on Ubuntu 14.04.2

在 Ubuntu 14.04.2 上测试

This was a base install of Ubuntu 14.04.2 on VirtualBox using ubuntu-trusty-64.

这是使用 ubuntu-trusty-64 在 VirtualBox 上基本安装的 Ubuntu 14.04.2。

I had just installed node and npm fresh and they were working well.

我刚刚安装了 node 和 npm fresh,它们运行良好。

npm install -y -g yo
echo export PATH="$HOME/npm/bin:$PATH" >> ~/.bashrc
npm config set prefix ~/npm
echo "export NODE_PATH=$NODE_PATH:/home/$USER/npm/lib/node_modules" >> ~/.bashrc && source ~/.bashrc
npm install -y -g yo

yes I had to install yeoman, fix the path issues, then reinstall yeoman.

是的,我必须安装 yeoman,修复路径问题,然后重新安装 yeoman。

The explanations are above from Oleg Tikhonov and bfred.it

以上解释来自 Oleg Tikhonov 和 bfred.it

回答by Joanna Marsden

After trying the above suggestion, I noticed that yeoman was installed, as expected, in /Users/joanna/.node/bin/yo. I set up symbolic links to yo, grunt, and bower in /usr/local/binwith

在尝试了上述建议后,我注意到 yeoman 已按预期安装在/Users/joanna/.node/bin/yo. 我成立了符号链接哟,咕噜和亭子/usr/local/bin

ln -s /Users/joanna/.node/bin/yo /usr/local/bin/yo
ln -s /Users/joanna/.node/bin/grunt /usr/local/bin/grunt
ln -s /Users/joanna/.node/bin/bower /usr/local/bin/bower

Everything works now, but I'm concerned that the links might cause problems in the future. Is there a better solution?

现在一切正常,但我担心这些链接将来可能会导致问题。有更好的解决方案吗?

回答by Jason

Just to add on,I am using OS X 10.11.1, the code as mentioned by steve works for me too.

补充一下,我使用的是 OS X 10.11.1,史蒂夫提到的代码也适用于我。

npm install -y -g yo
echo export PATH="$HOME/npm/bin:$PATH" >> ~/.bashrc
npm config set prefix ~/npm
echo "export NODE_PATH=$NODE_PATH:/home/$USER/npm/lib/node_modules" >> ~/.bashrc && source ~/.bashrc
npm install -y -g yo

回答by Arpit Aggarwal

Check where your npmis installed:

检查您npm的安装位置:

where npm

Running above command will list the directory where npmis, as follows:

运行上面的命令会列出所在的目录npm,如下:

/usr/local/Cellar/node/5.1.0/libexec/npm/bin/npm

Copy the path till binfolder and export the same as a PATHvariable, as below:

将路径复制到bin文件夹中,并将其导出为PATH变量,如下所示:

export PATH=$PATH:/usr/local/Cellar/node/5.1.0/libexec/npm/bin

回答by Badr Bellaj

In my case i had to execute :

就我而言,我必须执行:

npm config delete prefix

then set a nvm version

然后设置一个 nvm 版本

nvm use 8

and it works

它有效

回答by Despertaweb

I used :

我用了 :

$HOME/npm/bin

$HOME/npm/bin

And it pretty worked !

它非常有效!

In MAC OSX

MAC OSX 中