获取npm:未找到命令。当节点已经安装时,如何重新安装NPM ?NPM去了哪里?

时间:2022-03-18 07:15:40

I'm starting Learn to Build Modern Web Apps with the AngularJS Tutorial and 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,但是遇到了麻烦。我刚才重复了这些步骤,但是格朗特停止了工作,所以我重新开始。

I ran:

我跑:

$ sudo npm install -g generator-angular

and all the dependencies were installing until I received:

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

npm WARN package.json mongo@0.1.0 No repository field.
npm ERR! peerinvalid The package generator-karma does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer generator-angular@0.9.1 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:

然后我试着卸载生成者业力,重新开始:

$ 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.]

[编辑:Kudos to @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.

我的理解是在安装节点时安装了NPM,所以我的第二个问题是:如何仅重新安装NPM?我不希望从一开始就必须重新安装节点。

[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并不是一个选项,但是有很简单的方法重新安装两者。]

3 个解决方案

#1


37  

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

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

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

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

    不幸的是,npm更新-g没有实现任何人的期望。修复这一点在npm路线图上,但是这需要一段时间。

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

    几乎不需要全局安装包,除非(像generator- angle或grunt-cli)希望将该包用作命令。

#2


4  

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.

为Homebrew用户在Mac OS X上安装npm,运行以下命令删除所有现有的全局npm模块,卸载node和npm,以正确的默认值重新安装节点,将npm安装为自己的pacakge,并配置要安装的全局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。最后一步是添加~/。到路径的节点/bin可以使用全局安装的命令。我把这一行加到我的~中。路径脚本,通过~/.bash_profile运行。按原样运行以下一行。

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

#3


0  

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的用户。

#1


37  

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

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

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

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

    不幸的是,npm更新-g没有实现任何人的期望。修复这一点在npm路线图上,但是这需要一段时间。

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

    几乎不需要全局安装包,除非(像generator- angle或grunt-cli)希望将该包用作命令。

#2


4  

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.

为Homebrew用户在Mac OS X上安装npm,运行以下命令删除所有现有的全局npm模块,卸载node和npm,以正确的默认值重新安装节点,将npm安装为自己的pacakge,并配置要安装的全局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。最后一步是添加~/。到路径的节点/bin可以使用全局安装的命令。我把这一行加到我的~中。路径脚本,通过~/.bash_profile运行。按原样运行以下一行。

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

#3


0  

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的用户。