Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm command not found error but node is installed

I installed node and npm with Homebrew a while ago, they both worked fine until today when I keep running into the npm command not found error.

When is run $ whereis node, I get nothing back

When I do $ which node, I see /usr/local/bin/node

When I do $ node -v, I see v4.4.7

When I do $ whereis npm, I get nothing back

When I do $ which npm, I get nothing back

When I do $ npm -v, I see -bash: npm: command not found

I have tried

$ brew update
$ brew uninstall npm
$ brew install npm

I have also made sure that my $NODE_PATH environment variable is set:

# In ~/.bash_profile file:
export NODE_PATH="/usr/local/lib/node_modules"

I also followed these instructions from https://himanen.info/solved-npm-command-not-found/

Nothing seems to work and I keep getting npm: command not found when I run any command in any folder with npm. Any ideas? Thanks

like image 301
OneMoreQuestion Avatar asked Jul 20 '16 14:07

OneMoreQuestion


3 Answers

I had the same issue, I am using a MAC.
It was a permission issue in my case, here is what I already did:

$ brew update
$ brew uninstall npm
$ brew install npm  

That didn't work for me, so I tried this:

$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node

and this linked installed node with npm, when I typed:

$ npm -v
5.3.0

Now all commands followed by NPM are working fine,
like npm install

Hope this will work for all!!

like image 128
S.Yadav Avatar answered Sep 19 '22 02:09

S.Yadav


Figured out the issue. So the root of the problem was that I installed npm using Homebrew and there are some issues with what goes on under the hood with Homebrew and npm.

To fix this I did the following:

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

Important! Do this in .bash_profile

export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node/bin:$PATH"

Now everything works like a charm

like image 22
OneMoreQuestion Avatar answered Sep 21 '22 02:09

OneMoreQuestion


In mac via homebrew, when you are getting error like

Error: Permission denied @ dir_s_mkdir - /usr/local/lib/node_modules/npm

or mostly getting several folder permission, don't give full permission like

$ sudo chmod -R 777 /usr/local/lib

Please use as mentioned below

$ sudo chown -R $(whoami):admin /usr/local/lib/node_modules/

What it will do, simply gives the ownership to the user (linux users also can use this).

Hint: And in mac please use homebrew for installation. Advantages of homebrew you can switch between versions, easy to uninstall, you no need to run as root (sudo), like wise lots of advantages are there, as a developer its recommended to use homebrew (https://brew.sh/). And one more thing whenever you are getting some error like permission denied or something don't, give the full permission instead of using chmod use chown.

like image 30
Sridhar Avatar answered Sep 20 '22 02:09

Sridhar