62

I've installed node using brew install node and when I use the node -v I get following v5.0.0. However when I try to run the npm command I get npm: command not found.

I've tried to run brew install npm, however I just got following response node-5.0.0 already installed.

It happened with node also returning command not found, but that I fixed by running brew link node, however npm still does not seem to work.

How can I resolve this problem?

Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • do you try to run brew doctor? Remove completely nodejs from your system and try to reinstall again – vmkcom Nov 06 '15 at 20:56

7 Answers7

81

I encountered the same issue. After searching and reading different things online, what worked for me was:

$ brew postinstall node

However, first please do execute:

$ brew doctor

and follow the instructions there first, like the comment in your question mentions.

mpavel
  • 1,041
  • 1
  • 8
  • 7
  • 13
    For me this was due to a linking issue when running `brew install node`. After having successfully running `brew link node` I also needed to run `brew postinstall node` for it to add `npm` too. – RichieAHB Nov 25 '16 at 11:12
  • THANK YOU! you made my day sir! – rvandoni Jan 12 '18 at 15:41
  • After finding dozens of how-tos with hundreds of lines of instructions that go deep into the guts of Homebrew, this worked and should be the correct answer. – Zack Morris Mar 15 '18 at 21:46
  • 2
    UPDATE: Although this does work, running `brew remove --force yarn && brew remove --force node` then running `brew install node` fixed npm so that I didn't need to call `brew postinstall node` anymore. Then I called `brew install yarn`. So I think part of the problem was having multiple versions of yarn and/or node installed. – Zack Morris Mar 16 '18 at 19:51
  • `brew doctor` helped me remember I had contributed so my core repo was not in master, then I could reinstall node and it fixed it – Efren Jan 16 '20 at 22:25
54

Try running

$ brew postinstall node

If you ever ran sudo npm / sudo yarn, then you might need to change the owner of your global node_modules folder:

$ sudo chown -R "$(id -un)" "${NODE_PATH:-/usr/local/lib/node_modules}"
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
49

Try the following:

$ brew update
$ brew uninstall node
$ brew install node
$ sudo chown -R YourLocalUserName /usr/local/lib/node_modules/
$ brew postinstall node

Sun
  • 2,110
  • 2
  • 21
  • 28
dian.in
  • 491
  • 4
  • 2
3

the same problem with you, I fixed by:

/usr/local/lib/node_modules/npm/bin/npm-cli.js i -g npm

zhulinpinyu
  • 599
  • 7
  • 12
0

I used cyrillic (non-latin) characters in my hostname/computername on mac. And that was the problem. After i changed this and reinstalled node/npm with Homebrew everything worked like normal. No utf8 in 2017? :/

tedi
  • 6,350
  • 5
  • 52
  • 67
0

running: echo 'export PATH="/opt/homebrew/opt/node@16/bin:$PATH"' >> ~/.zshrc should work, change node@16 to just node or which ever version you just installed.

Pianisimo
  • 99
  • 1
  • 6
0

I fixed it by running brew link node@18, remember to use @ if you installed a specific version from the brew, otherwise brew link node should work.

If you are not sure what version you install, you can go to the folder /opt/homebrew/Cellar/ to check what version of node you have installed.

dexbyte
  • 65
  • 11