node.js zshrc“找不到命令:npm”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15777660/
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 14:05:24  来源:igfitidea点击:

zshrc "command not found: npm"

bashnode.jsnpmzsh

提问by Sasha

So I'm looking into learning node.js. I've been following this tutorialto install it and try some stuff out.

所以我正在研究学习 node.js。我一直在按照本教程安装它并尝试一些东西。

At any rate, npm is behaving really strangely. It'll work just fine for a while (I changed its viewer config settings, for instance), but then suddenly 'disappear', so that the next time I enter

无论如何,npm 的行为真的很奇怪。它会工作一段时间(例如,我更改了它的查看器配置设置),但突然“消失”,以便下次我输入

npm ...(anything)

in the command line, my shell informs me that the npm command has not been found. Previously, uninstalling and then reinstalling npm using nvm has worked, but I'd rather not do this every ten minutes, and it's super weird to me that it will suddenly stop working.

在命令行中,我的 shell 通知我未找到 npm 命令。以前,使用 nvm 卸载然后重新安装 npm 是有效的,但我不想每十分钟执行一次,而且它会突然停止工作对我来说非常奇怪。

I think the last time, it stopped working after I edited and then re-sourced my ~/.zshrc file -- just added npm and node tab-completion plugins.

我想最后一次,它在我编辑然后重新获取我的 ~/.zshrc 文件后停止工作 - 刚刚添加了 npm 和 node tab-completion 插件。

Any idea what's going on? I'll post the results of a couple basic commands, but I'm really bad with sysadmin stuff, and I don't really know what to look for here.

知道发生了什么吗?我将发布几个基本命令的结果,但我对 sysadmin 的东西真的很糟糕,而且我真的不知道在这里寻找什么。

nvm -ls

nvm -ls

   v0.8.22
current:    v0.10.2
default -> 0.8 (-> v0.8.22)

which node

哪个节点

/usr/bin/node

which npm

哪个 npm

npm not found

There's are both nvm (is this a duplicate?) and npm directories directly in my root folder.

我的根文件夹中直接有 nvm(这是重复的吗?)和 npm 目录。

If I go into /usr/bin, node is a file, not a directory. There's also a file npm, but not file nvm.

如果我进入 /usr/bin,节点是一个文件,而不是一个目录。还有一个文件 npm,但没有文件 nvm。

As all the above probably shows, I really have no idea what's going on. I've searched for this, but nobody seems to have this problem. Any ideas what's going on or how to find out?

正如以上所有可能表明的那样,我真的不知道发生了什么。我已经搜索过这个,但似乎没有人有这个问题。任何想法发生了什么或如何找出?

EDITNoticing that my default node isn't up-to-date, I did all this. Putting aside why my shell switched me automatically to the bin directory (and why that seems to be a github repo -- no idea what's up with my computer), does this provide any clues?:

编辑注意到我的默认节点不是最新的,我做了所有这些。撇开为什么我的 shell 自动将我切换到 bin 目录(以及为什么这似乎是一个 github 存储库 - 不知道我的电脑怎么了),这是否提供了任何线索?:

?  node  nvm alias default v0.10.2
default -> v0.10.2
?  node  nvm ls
   v0.8.22
current:    v0.10.2
default -> v0.10.2
?  node  npm
zsh: command not found: npm
?  node  nvm install npm
######################################################################## 100.0%
Checksums do not match.
Binary download failed, trying source.
Additional options while compiling: 
nvm: install N/A failed!
?  bin git:(master) nvm uninstall npm
N/A version is not installed yet... installing
######################################################################## 100.0%
Checksums do not match.
Binary download failed, trying source.
Additional options while compiling: 
nvm: install N/A failed!
?  bin git:(master) cd ~/rails/node

EDIT 2- I made sure the node path was added to my zshrc file:

编辑 2- 我确保节点路径已添加到我的 zshrc 文件中:

#other stuff...
export PATH=(...):/usr/bin/node

But when I then tried to run

但是当我然后尝试运行时

source ~./zshrc

I got this complaint:

我收到了这样的投诉:

.zshrc:11: command not found: npm

which points to these lines (numbers added):

指向这些行(添加数字):

10 . ~/nvm/nvm.sh
11 . <(npm completion)

which I got from following the above tut and, back when npm was working, looking up how to implement tab completion.

我从上面的 tut 得到了,回到 npm 工作时,查找如何实现选项卡完成。

In response to Jens

回应 Jens

This is in my ~/nvm/nvm.sh file, near the start:

这是在我的 ~/nvm/nvm.sh 文件中,靠近开始处:

# Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then
    export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-
nano ~/.zshrc
}) && pwd) fi # Make zsh glob matching behave same as bash # This fixes the "zsh: no matches found" errors if [ ! -z "$(which unsetopt 2>/dev/null)" ]; then unsetopt nomatch 2>/dev/null fi

I tried to (and I think successfully did) chmod the npm directory to make it read/write/executable.

我尝试(并且我认为成功)修改了 npm 目录以使其可读/写/可执行。

回答by egyamado

I had the issue where neither Node nor npm works. I spent about 6 hours searching for solution online, but no luck.

我遇到了 Node 和 npm 都不工作的问题。我花了大约 6 个小时在网上寻找解决方案,但没有运气。

As npm websitemention:

正如npm 网站提到的:

npm comes with nodenow.

npm现在带有节点

I thought to install Node.js again, with a hope it will fix these issues, and it did :)

我想再次安装 Node.js,希望它能解决这些问题,它确实做到了 :)

After installation was completely successful. Node installer shows this final message:

安装完全成功后。节点安装程序显示此最终消息:

Node was installed at

/usr/local/bin/node

npm was installed at

/usr/local/bin/npm

Make sure that /usr/local/bin is in your $PATH.

节点安装在

/usr/local/bin/node

npm 安装在

/usr/local/bin/npm

确保/usr/local/bin is in your $PATH.

And that's where I notice that my $PATHand installation directories were wrong. After installation I'm abel to run node and npm.

这就是我注意到我的$PATH和安装目录错误的地方。安装后,我可以运行 node 和 npm。

Hope it helps...

希望能帮助到你...

回答by zarpio

1) Open your zsh config file

1)打开你的zsh配置文件

export PATH="$PATH:/usr/local/share/npm/bin"

2) Add path

2) 添加路径

source ~/.zshrc

3) Save the file by pressing CTRL + X and then Y

3) 按 CTRL + X 然后按 Y 保存文件

4) Executes the content of the file

4) 执行文件内容

source ~/.bash_profile

This simple process worked for me :-)

这个简单的过程对我有用:-)

回答by Atul Kumar

I had the same problem with my Mac, I did the following:

我的 Mac 遇到了同样的问题,我做了以下操作:

  1. Uninstall the existing node
  2. Go to the node webpage and download the package, then install it
  3. Go to ~/.zshrcand the path would usually be /usr/local/bin
  4. Have fun with npm
  1. 卸载现有节点
  2. 去节点网页下载包,然后安装
  3. 去,~/.zshrc路径通常是/usr/local/bin
  4. 从...中得到快乐 npm

回答by nmu

For me, and other googlers arriving here, the problem was that when calling sudo on Ubuntu, secure_pathwas being used, which didn't include NPM.

对于我和其他到达这里的谷歌员工来说,问题是在 Ubuntu 上调用 sudo 时,secure_path正在使用,其中不包括 NPM。

You can either disable secure_Path, which definitely doesn't seem recommended. So instead I just appended the node bin to secure path, when installed via NVM it looked like this for me

您可以 disable secure_Path,这似乎绝对不推荐。因此,我只是将节点 bin 附加到secure path,当通过 NVM 安装时,对我来说看起来像这样

  • $: sudo su

  • $: visudo

  • $:须藤苏

  • $:视觉

Then adding /home/YourUsername/.nvm/versions/node/v8.5.0/bin:to the secure_pathvariable.

然后添加/home/YourUsername/.nvm/versions/node/v8.5.0/bin:secure_path变量中。

Further info: sudo changes PATH - why?

更多信息:sudo 更改路径 - 为什么?

回答by Shiroi Okami

Adding this to my ./zshrc file has helped me on ubuntu 19.10:

将此添加到我的 ./zshrc 文件对我在 ubuntu 19.10 上有所帮助:

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && 。“$NVM_DIR/nvm.sh”

Source: https://superuser.com/questions/1403007/zsh-npm-node-nvm-command-not-found-after-installing-ohmyzsh

来源:https: //superuser.com/questions/1403007/zsh-npm-node-nvm-command-not-found-after-installing-ohmyzsh

回答by tklives

Honestly, not sure if this will/would solve the problem you are/were having, but I was experiencing pretty much exactly what you detailed - where npm was working, but then randomly just completely stopped working - and I tried a ton of the same things people suggested in here with no luck.

老实说,不确定这是否会/会解决您/正在遇到的问题,但是我几乎完全按照您的描述进行了体验-npm 在何处工作,但随后随机完全停止工作-我尝试了很多相同的问题人们在这里建议的事情没有运气。

What ended up doing the trick for me, as simple/dumb as this sounds/is, was:

最终为我做的伎俩,就像这听起来一样简单/愚蠢,是:

curl -L https://npmjs.org/install.sh | sh

I wish I had a better explanation as to why that was the magic bullet, but at least it works!

我希望我能更好地解释为什么那是灵丹妙药,但至少它有效!

Cheers!

干杯!

回答by DannyFeliz

##代码##

I tried it on a fresh CentOS 7 install and it works!

我在全新的 CentOS 7 安装上进行了尝试,并且可以正常工作!