node.js npm 命令未找到错误但节点已安装

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

npm command not found error but node is installed

node.jsbashnpmhomebrewnpm-install

提问by OneMoreQuestion

I installed node and npm with Homebrew a while ago, they both worked fine until today when I keep running into the npm command not founderror.

不久前我用 Homebrew 安装了 node 和 npm,直到今天我一直遇到npm 命令未找到错误时,它们都运行良好。

When is run $ whereis node, I get nothing back

何时运行$ whereis node,我一无所获

When I do $ which node, I see /usr/local/bin/node

当我执行$ which node 时,我看到/usr/local/bin/node

When I do $ node -v, I see v4.4.7

当我执行$ node -v 时,我看到v4.4.7

When I do $ whereis npm, I get nothing back

当我执行$ whereis npm 时,我一无所获

When I do $ which npm, I get nothing back

当我执行$ which npm 时,我一无所获

When I do $ npm -v, I see -bash: npm: command not found

当我执行$ npm -v 时,我看到-bash: npm: command not found

I have tried

我试过了

$ brew update
$ brew uninstall npm
$ brew install npm

I have also made sure that my $NODE_PATHenvironment variable is set:

我还确保设置了我的$NODE_PATH环境变量:

# In ~/.bash_profile file:
export NODE_PATH="/usr/local/lib/node_modules"

I also followed these instructions from https://himanen.info/solved-npm-command-not-found/

我还遵循了https://himanen.info/solved-npm-command-not-found/ 中的这些说明

Nothing seems to work and I keep getting npm: command not foundwhen I run any command in any folder with npm. Any ideas? Thanks

似乎没有任何效果,当我使用 npm 在任何文件夹中运行任何命令时,我不断收到npm: command not found。有任何想法吗?谢谢

回答by OneMoreQuestion

Figured out the issue. So the root of the problem was that I installed npm using Homebrew and there are some issues with what goes on under the hood with Homebrew and npm.

想通了这个问题。所以问题的根源是我使用 Homebrew 安装了 npm,并且 Homebrew 和 npm 的幕后发生了一些问题。

To fix this I did the following:

为了解决这个问题,我做了以下事情:

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

Important!Do this in .bash_profile

重要的!在 .bash_profile 中执行此操作

export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node/bin:$PATH"

Now everything works like a charm

现在一切都像魅力一样

回答by S.Yadav

I had the same issue, I am using a MAC.
It was a permission issue in my case, here is what I already did:

我有同样的问题,我正在使用MAC。
在我的情况下,这是一个许可问题,这是我已经做的:

$ brew update
$ brew uninstall npm
$ brew install npm  

That didn't work for me, so I tried this:

这对我不起作用,所以我尝试了这个:

$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node

and this linked installed node with npm, when I typed:

当我输入以下内容时,这个链接的已安装节点与 npm 连接:

$ npm -v
5.3.0

Now all commands followed by NPM are working fine,
like npm install

现在 NPM 后面的所有命令都可以正常工作,
例如npm install

Hope this will work for all!!

希望这对所有人都有效!!

回答by Sridhar

In mac via homebrew, when you are getting error like

在 mac 中通过自制软件,当你收到类似的错误时

Error: Permission denied @ dir_s_mkdir - /usr/local/lib/node_modules/npm

or mostly getting several folder permission, don't give full permission like

或者主要是获得多个文件夹权限,不要给予完全权限,例如

$ sudo chmod -R 777 /usr/local/lib

Please use as mentioned below

请按如下方式使用

$ sudo chown -R $(whoami):admin /usr/local/lib/node_modules/

What it will do, simply gives the ownership to the user (linux users also can use this).

它会做什么,只是将所有权授予用户(Linux 用户也可以使用它)。

Hint: And in mac please use homebrew for installation. Advantages of homebrew you can switch between versions, easy to uninstall, you no need to run as root (sudo), like wise lots of advantages are there, as a developer its recommended to use homebrew (https://brew.sh/). And one more thing whenever you are getting some error like permission denied or something don't, give the full permission instead of using chmoduse chown.

提示:在 mac 中请使用 homebrew 进行安装。自制软件的优点,您可以在版本之间切换,易于卸载,您无需以 root 身份运行(sudo),就像有很多优点一样,作为开发人员,建议使用自制软件(https://brew.sh/) . 还有一件事,每当您遇到诸如权限被拒绝或某些错误之类的错误时,请提供完整权限,而不是使用chmoduse chown

回答by Rajeev Jayaswal

I had the same issue, I executed following command to install node and npm - it worked perfectly.

我遇到了同样的问题,我执行了以下命令来安装 node 和 npm - 它运行良好。

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node