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

3

18

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

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.

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:

$ 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?

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

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.

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

Shannonshanny answered 24/6, 2014 at 4:27 Comment(7)
I don't think its possible. Only possible answer is to reinstall node completely.Boswell
@Boswell Thanks. Thats a shame. It would be great to figure out how and why this happened so I can avoid it in the future.Shannonshanny
Why have you mixed up the commands as normal user and root? $ npm update -g as you wrote looks wrong, if you deal with global packages, you'll need to run this as root too. Funny: you then used sudo but did another npm command. Maybe you've messed up the user environment? Can you please check that /usr/local/bin/npm is really not existing? Please try to find /usr it. On the original error Peer [email protected] wants generator-karma@>=0.8.2: It looks like the generator-angular guys updated the dependencies which are not present in the repo (I came across such problems).Peden
@Peden I mixed up the commands as normal user and root because I'm new to this and absent-minded :). $ find/usr npm -bash: find/usr: No such file or directory` Good to know that I'm not the only one that run into the issues with updated dependencies not in the repo. Thanks!Shannonshanny
@Rotimi: +1 for finding out the reason to avoid it in the future! Unfortunately if something really messed up, you'll need t0 reinstall and then try to reproduce your steps observing the system meanwhile. :/ -- Have you built node + npm on your own using ./configure; make; make install? Who owns /usr/local/bin/npm? root.root?Peden
find /usr (a space between find and the path)! Please fly-in your mind ;) Please also issue whereis node and whereis npm too to show all path providing node and npm. --- Please add your observations to your post by editing it!Peden
Thanks, edited the post. I'm not sure I understand how to issue those whereis commands. As for the ./configure; make; make install, is this the first option in this link?Shannonshanny
N
41
  1. If you have a working node, you can re-install npm

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.

  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.

Nickelic answered 21/11, 2014 at 17:5 Comment(0)
P
4

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

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.

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.

export PATH="$HOME/.node/bin:$PATH"
Poulard answered 9/12, 2015 at 16:3 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewFeisty
@ValentinLorentz my apologies, there is the update. Let me know if it looks good.Poulard
M
0

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

    npm install npm@latest -g

after some tries i found the solution:

    yum reinstall npm

I hope this could help redhat/centos users.

Monodic answered 24/11, 2017 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.