npm command not found error but node is installed

33,438

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
33,438
OneMoreQuestion
Author by

OneMoreQuestion

Trying to learn as much as possible whenever I can

Updated on July 09, 2022

Comments

  • OneMoreQuestion
    OneMoreQuestion almost 2 years

    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

  • OneMoreQuestion
    OneMoreQuestion almost 8 years
    This is to fix the issue if Node and NPM paths are already messed up
  • SSS
    SSS over 5 years
    Worked for me. Thanks :)
  • Goor Lavi
    Goor Lavi over 4 years
    Solved my issue. Thanks!
  • Mike Chen
    Mike Chen over 2 years
    This works like a charm