NPM Command Not Found After Installing Node

2019-02-07 18:31发布

问题:

I am having a very hard time getting the npm command to work, and unfortunately my knowledge of unix isn't good enough to solve this on my own. All I've done is brew install node, and I get the following errors:

When I type npm I get zsh: command not found: npm

Looking into this issue more I found this stack overflow answer:

Command not found after npm install in zsh

Following its advice I tried adding export PATH=/usr/local/share/npm/bin:$PATH to my .zshrc file. Still get the same error when typing npm

I can confirm it is in the path echo $PATH yields /Users/nicholashaley/.rbenv/shims:/Users/nicholashaley/.rbenv/bin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/usr/local/heroku/bin:/usr/local/share/npm/bin:/usr/local/bin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/usr/local/heroku/bin:/Users/nicholashaley/.rbenv/shims:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

I can also confirm that node has been installed node -v yields v0.12.2

Any ideas? I'm stumped

回答1:

Not an answer, but maybe a solution...

Uninstall Node via brew: brew uninstall node

Then download Node from https://nodejs.org/en/download/ and install.

npm should then "just work".



回答2:

I came across to this thread with the same problem but finally below is what worked for me which is without sudo

$ brew update
$ brew uninstall node
$ brew install node
$ chown -R YourUserName /usr/local/lib/node_modules/
$ brew postinstall node #This what the important step


回答3:

Ok this is what worked for me:

$ brew update
$ brew uninstall node
$ brew install node
$ sudo brew postinstall node #This what the important step


回答4:

In case this never got solved, this is what worked for me:

  1. brew uninstall node
  2. brew install node
  3. brew doctor and see what needs to be pruned. My problem was there was a non-brew place where I had changed my install location for npm packages because I was trying to get an npm package to install for me. That was a mistake (having forgotten I initially installed node and npm through brew but now brew could no longer reach the npm install directory nor could it change or delete it). So brew prune didn't work even though the symlinks were supposedly deleted but it reminded me that I had changed the npm install location. And that brings us to:

  4. Delete those directories

  5. brew postinstall node

Hope it helps.



回答5:

Anyone on the LTS release use the following command instead:

brew postinstall node@6