38

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

Community
  • 1
  • 1
Nicholas Haley
  • 3,558
  • 3
  • 29
  • 50
  • Uninstalling and reinstalling Node didn't work so I wiped out the /usr/local/lib/node_modules dir (with sudo rm -rf /usr/local/lib/node_modules) fixed it. – Warren P Feb 19 '17 at 03:12

8 Answers8

48

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

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
  • 1
    before trying this I tried brew postinstall but didnt worked well, above method is better because alongwith node it also takes care of npm many thanks – Pravin W Feb 08 '17 at 12:44
  • 1
    This didn't work, but once I `sudo rm -rf /usr/local/lib/node_modules` just after uninstall, then it worked. – BrotherDonkey Jan 27 '18 at 23:32
42

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
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
  • 1
    in my case `postinstall` ask for sudo rights and brew does not like it :( – Volatil3 May 15 '16 at 11:54
  • 2
    this one helps, but I should do http://stackoverflow.com/questions/23400073/npm-not-working-after-reinstalling-mac-os-x#comment41702313_23421019 before final step – yanik May 27 '16 at 19:00
  • 1
    Error: /usr/local is not writable. You should change the ownership and permissions of /usr/local back to your user account: – Rajesh Maurya May 03 '18 at 08:54
  • @Venkat.R Thank you so much bro. I am new to mac and you saved lots of time. – Abhishek Shukla Jul 03 '19 at 04:33
  • For the `chown` command I needed to prefix it with `sudo` otherwise the operation is not permitted. This fixed my homebrew installed node/npm, thank you. – JulianHarty Jul 17 '19 at 10:53
18

Ok this is what worked for me:

$ brew update
$ brew uninstall node
$ brew install node
$ sudo brew postinstall node #This what the important step
Nicholas Haley
  • 3,558
  • 3
  • 29
  • 50
  • 8
    sudo brew does not look like a good idea... Here is the error I get: `Error: Cowardly refusing to \`sudo brew postinstall\` You can use brew with sudo, but only if the brew executable is owned by root. However, this is both not recommended and completely unsupported so do so at your own risk.` – Litome Nov 05 '15 at 16:25
  • ^ agreed - you shouldn't need to run `$ sudo brew ...`. – Andrew Hendrie Feb 17 '16 at 14:27
  • I'm also facing the same issue. not sure. after following 3 steps. still my npm command not found – Venkat.R Feb 29 '16 at 22:38
  • without sudo of 4th command solved my problem. let me update as a separate answer for reference – Venkat.R Feb 29 '16 at 22:55
  • In my case I needed to postinstall the exact version: `brew postinstall homebrew/versions/node4-lts;` – ownking Jun 08 '16 at 23:21
  • 1
    postinstall did the trick for me, but no need for sudo. instead, chown the inaccessible files/dirs to your own admin user (me:admin). – yair Feb 22 '17 at 16:16
9

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.

keaglin
  • 317
  • 4
  • 10
4

I found this article helpful with step-by-step details:
https://www.dyclassroom.com/howto-mac/how-to-install-nodejs-and-npm-on-mac-using-homebrew

Additionally, after all the steps in above article, node was working but my machine was still giving "command not found: npm". I then ran the following two commands and npm was working.

sudo chown -R myusername:myusergroup /usr/local/lib/node_modules
brew postinstall node
Vaibhav Rathore
  • 301
  • 2
  • 10
  • This works just fine. sometimes, brew postinstall node doesn't run. So, after running the first command, node -v will get you the right node version, However you will npm and it will return "npm not found" on trying npm command. "brew postinstall node" will fix that for you – Balu Mar 26 '20 at 14:46
  • After trying all the above steps. This finally worked. Thank you. – demiculus Dec 27 '21 at 08:19
0

Anyone on the LTS release use the following command instead:

brew postinstall node@6
mils
  • 1,878
  • 2
  • 21
  • 42
0

Issue Details in Gist and how I fixed it

  1. Installed nodeJs via nvm using Git Bash
  2. Installed hyper terminal & WSL
  3. Installed Ubuntu (windows Store) & configured zsh shell. 
  4. Faced the command not found all over in the zsh shell
  5. The same commands are working nicely in the bash shell

Tried sourcing the (node, nvm, npm ) paths in the .zshrc and .profile file & modified the System & Environment Variables in Windows. None of these steps helped me to resolve the issue.

I think the issue is zsh and bash terminals treat things differently. When Installed in bash terminal the default location for the nodeJs in windows will be set to

C:\Users\Program Files\nodejs

The above one is symlinked to C:\Users\<username>\AppData\Roaming\nvm

No matter how much I tried to alias these things in zsh, only node and nvm are working with aliases. npm is failing to locate the necessary lib files.

The only solution that worked for me is Re-installing node in zsh terminal

  1. Removed the node versions installed via nvm in Bash terminal.
  2. Removed the nvm in windows via control Panel
  3. Installed the nvm in the zsh shell
  4. Installed the node latest LTS version from which npm is also installed.

Post Installing the node via nvm in Zsh I observed the following things.

  1. Paths for the node, nvm, npm & npx have been automatically added to the $PATH variable
  2. Node directory is not available neither in C:\Users\Program Files\nodejs nor the C:\Users\{username}\AppData\Roaming\nvm
  3. Instead it is configured differently at /home/<username>/.nvm/versions/node/v14.15.1/bin/node

This video really helped me in re-installing the node in ZSH: https://www.youtube.com/watch?v=kL8iGErULiw

0

Usually npm should be found in the same directory as that of the node executable, which can be determined by which node. Check if this directory is part of your PATH. If node was installed through a node version managers such as nodenv and nvm, check the documentation for how to add these executables to the path.

With nodenv, this can be done with:

eval "$(nodenv init -)"
ealfonso
  • 6,622
  • 5
  • 39
  • 67