npm command not found error but node is installed

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!!


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


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.