node.js 获取 npm:找不到命令。已安装 Node 后如何重新安装 NPM?NPM 去哪儿了?

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

Getting npm: command not found. How do I reinstall NPM when Node is already installed? Where did NPM go?

node.jsangularjsnpmboweryeoman-generator-angular

提问by Rotimi

I'm starting Learn to Build Modern Web Apps with the AngularJS Tutorialand running into issues very early.

我开始学习使用 AngularJS 教程构建现代 Web 应用程序,并很早就遇到了问题。

I have node installed:

我安装了节点:

/path/ang-news node -v
v0.10.26

I was using NPM earlier but was running into trouble with Yeoman. I've repeated these steps a while back but Grunt stopped working so I started fresh.

我之前使用过 NPM,但在使用 Yeoman 时遇到了麻烦。我已经重复了这些步骤,但 Grunt 停止工作,所以我重新开始。

I ran:

我跑了:

$ sudo npm install -g generator-angular

and all the dependencies were installing until I received:

并且所有依赖项都在安装,直到我收到:

npm WARN package.json [email protected] No repository field.
npm ERR! peerinvalid The package generator-karma does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants generator-karma@>=0.8.2

I then tried updating:

然后我尝试更新:

$ npm update -g

I should have run this as an administrator. I received tons of error messages, this seemed most pertinent:

我应该以管理员身份运行它。我收到了大量错误消息,这似乎最相关:

npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 13.1.0
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "update" "-g"
npm ERR! cwd /path/ang-news
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3

npm ERR! not ok code 0

Then I tried uninstalling generator-karma and starting fresh:

然后我尝试卸载 generator-karma 并重新开始:

$ sudo npm uninstall -g generator-karma

but received:

但收到:

sudo: npm: command not found

$ npm -v
-bash: /usr/local/bin/npm: No such file or directory

My first question is: Why did NPM suddenly disappear?

我的第一个问题是:为什么 NPM 突然消失了?

[EDIT: Kudos to @try-catch-finally to pointing out the havoc that ensues when switching between normal user and sudo when issuing commands. It turns out that I messed up my user environment and NPM was no longer installed.]

[编辑:感谢@try-catch-finally 指出在发出命令时在普通用户和 sudo 之间切换时会发生的破坏。原来是我把我的用户环境搞乱了,NPM也没有安装了。]

My understanding is that NPM is installed when you install Node, so my second question is: How do I reinstall just NPM?I'd prefer not to have to reinstall Node from the beginning.

我的理解是安装Node的时候就安装了NPM,所以我的第二个问题是:如何只重新安装NPM?我不想从一开始就重新安装 Node。

[EDIT: Kudos to @hawk and @try-catch-finally - it doesn't appear that installing NPM alone is an option, but there are easy ways to reinstall both.]

[编辑:感谢@hawk 和@try-catch-finally - 单独安装 NPM 似乎不是一种选择,但有一些简单的方法可以重新安装两者。]

回答by Sam Mikes

  1. If you have a working node, you can re-install npm
  1. 如果你有一个工作节点,你可以重新安装 npm

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

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

  1. Unfortunately npm update -gdoes not do what anybody expects. Fixing this is on the npm roadmap, but it's going to take a while.

  2. You almost never need to install a package globally, unless (like generator-angularor grunt-cli) you want to use that package as a command.

  1. 不幸的npm update -g是没有做任何人所期望的。解决这个问题是在 npm 路线图上,但这需要一段时间。

  2. 您几乎不需要全局安装包,除非(如generator-angulargrunt-cli)您想将该包用作命令。

回答by alphapilgrim

Just in case you've done this with brew, I recommend this article on github. Will save you a lot of time.

以防万一你用 brew 完成了这个,我推荐 github 上的这篇文章。会为你节省很多时间。

https://gist.github.com/DanHerbert/9520689

https://gist.github.com/DanHerbert/9520689

Fixing npm On Mac OS X for Homebrew Users Run the following commands to remove all existing global npm modules, uninstall node & npm, re-install node with the right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

在 Mac OS X 上为 Homebrew 用户修复 npm 运行以下命令以删除所有现有的全局 npm 模块,卸载节点和 npm,使用正确的默认值重新安装节点,将 npm 安装为自己的包,并配置全局 npm 模块的位置要安装。

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

Node and npm should be correctly installed at this point. The final step is to add ~/.node/bin to your PATH so commands you install globally are usable. I added this line to my ~/.path script, which gets run via ~/.bash_profile. Run the following line as is.

此时应正确安装 Node 和 npm。最后一步是将 ~/.node/bin 添加到您的 PATH 中,以便您全局安装的命令可用。我将此行添加到我的 ~/.path 脚本中,该脚本通过 ~/.bash_profile 运行。按原样运行以下行。

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

回答by Zhi Yuan

I met the exactly same problem after execute command to install the npm with latest version on redhat 7.1:

在执行命令以在redhat 7.1上安装最新版本的 npm 后,我遇到了完全相同的问题:

    npm install npm@latest -g

after some tries i found the solution:

经过一些尝试,我找到了解决方案:

    yum reinstall npm

I hope this could help redhat/centos users.

我希望这可以帮助 redhat/centos 用户。