Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 284
Rotimi Avatar asked Jun 24 '14 04:06

Rotimi


People also ask

How will you resolve the error npm command not found?

The npm command not found error js server, which you can download from the nodejs.org website. Once you downloaded and installed Node. js, open the terminal and run the npm -v command. Once you see the npm version, you should be able to run the npm install command again.

Where install npm install?

npm install -g pm2 - pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules (Use npm root -g to check where.) If you're using nvm, then your global modules may be in one of several places depending on the version of node you're using at the time.

How do you check if npm is already installed?

Test NPM. To see if NPM is installed, type npm -v in Terminal.


2 Answers

  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.

like image 193
Sam Mikes Avatar answered Oct 21 '22 04:10

Sam Mikes


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"
like image 28
alphapilgrim Avatar answered Oct 21 '22 04:10

alphapilgrim