65

I tried to fix the error where you have to use sudo when running npm. I blindly followed a link to uninstall node, the code was from this gist

After running the command and I tried to install it back with brew: brew install node. Which gave me the following error:

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/doc/node/gdbinit
/usr/local/share/doc/node is not writable.

You can try again using:
  brew link node

Trying to run brew link node, I got:

Linking /usr/local/Cellar/node/5.4.0... 
Error: Could not symlink share/systemtap/tapset/node.stp
/usr/local/share/systemtap/tapset is not writable.

Then when I write brew install npm, I get:

Warning: node-5.4.0 already installed, it's just not linked

When I write npm -v I get:

env: node: No such file or directory

Any ideas on how to solve this?

6
  • That gist is designed for the pkg installer, not Homebrew. You might try removing node with brew remove node, then reinstalling. Commented Feb 7, 2016 at 3:04
  • @AlexanderO'Mara I did initially install it with pkg installer, then tried with brew. Running brew remove node didn't work. Installing brew again still gives the link error
    – Ela
    Commented Feb 7, 2016 at 3:08
  • 1
    Run brew doctor and see what warnings you get. Sounds like permissions may have been changes. I would also try manually deleting the /usr/local/Cellar/node/ folder if hombrew cannot remove it. Commented Feb 7, 2016 at 3:11
  • I was able to fix this issue by running sudo chown -R $USER /usr/local and than brew link node. Is this a valid fix? Should I give it as an answer or just delete the question?
    – Ela
    Commented Feb 7, 2016 at 3:27
  • I believe that's what Homebrew itself does to setup permissions, so it should be right. As for making an answer or deleting, I don't personally mind one way or the other. You might consider the possibility of it helping future visitors. Commented Feb 7, 2016 at 3:29

17 Answers 17

83

I managed to fix this by first running sudo chown -R $USER /usr/local and following it with brew link node. Now I have node properly installed.

8
  • after running this shouldn't you switch the ownership back to root with sudo chown -R root /usr/local? seems like that directory shouldn't be left under the user Commented Feb 23, 2018 at 18:30
  • 41
    Can't chown /usr/local in OSX High Sierra. Instead, use this sudo chown -R $(whoami) $(brew --prefix)/*. See github.com/Homebrew/brew/issues/3228
    – markhops
    Commented Mar 16, 2018 at 13:00
  • 11
    I needed to execute: brew link --overwrite node
    – vikzilla
    Commented May 31, 2018 at 3:59
  • 4
    markhops and vikzilla 's comments above were the only thing that worked for me on Mojave 10.14.2 Commented Feb 28, 2019 at 15:39
  • @markhops & vikzilla fixed it for me as well on Mojave 10.14.6, thanks
    – Adibe
    Commented Dec 30, 2019 at 21:56
48

This solution works! It is a combination of all the combinations.

Fix it using the following code.

  1. sudo chmod 776 /usr/local/lib
  2. brew link --overwrite node

    Outputs: Linking /usr/local/Cellar/node/9.6.1... 49 symlinks created

  3. sudo chmod 755 /usr/local/lib
4
  • Thanks! This is the only solution that worked for me. Commented Feb 25, 2020 at 13:23
  • Thank you! this fixed it for me.
    – Ken Ryan
    Commented Mar 18, 2020 at 18:32
  • THANK YOU!!!!!! 👏👏👏👏👏👏
    – neoswf
    Commented Dec 13, 2021 at 2:20
  • Worked perfectly! Commented Jan 25, 2022 at 23:11
23

For Mojave || Catalina || Big Sur || Monterey || Ventura use:

sudo chown -R $(whoami) $(brew --prefix)

brew link --overwrite node
1
  • 2
    This fixed it for me on macOS Catalina. Thanks! Commented Nov 6, 2019 at 17:29
16

for OSX High Sierra users:

sudo chown -R $(whoami) $(brew --prefix)/*

brew link --overwrite node

then check node -v and npm -v

1
  • October 2020! - This is it. Although I don't understand what chown or whoami does, this worked. Thanks!
    – fourOhFour
    Commented Oct 8, 2020 at 21:27
13

If you are on High Sierra, then sudo chown -R $USER /usr/local won't work. Instead use:

sudo chown -R $(whoami) $(brew --prefix)/*

Source: https://github.com/Homebrew/brew/issues/3228#issuecomment-332679274

11
sudo chown -R $USER /usr/local
brew link --overwrite node
7

You have to allow it to be overwritten. Whatever directory is not writable

For example, if its /usr/lib/dtrace

sudo chown -R `whoami`:admin /usr/lib/dtrace

And then

brew link --overwrite node

Fixes it

5

Change /usr/local permission to your user:

sudo chown -R $USER /usr/local

Then run brew postinstall:

brew postinstall node

Now you're done. To check:

npm -v

Extra step: run brew doctor for cleaning purpose

brew doctor

You might need to prune some extra old stuff if doctor find it.

4

In this case, Apparently you're struggling with 2 versions in your machine and You should link pointer to the right version, Try installing again node and Unlink the previous version and Link the new one.

brew install node
brew link --overwrite node

//If it already linked
brew unlink node && brew link node
2

I did have the same issue when I was brew link jq but refering to /usr/local/lib is not writable. In one of my machines I couldn't apply Ela suggestion. I did sudo chmod 776 /usr/local/lib then I did brew link jq which was successful and then changed it back to sudo chmod 755 /usr/local/lib.

2

Nothing worked for me except

brew cleanup
0
1

sudo chown -R $USER /usr/local Avoid this. Playing with ownership is tricky can be catastrophic for noobs.

If you are using Home-brew do not install node and rpm separately.

First install Homebrew and then use brew to install all the package.

Solution - If you install node and npm separately and then also install through home-brew then you will get permissions issues- i was installing React Native CLI when faced this issue. Remove all the below mentioned folders. /usr/local/bin,/usr/local/etc,/usr/local/include,/usr/local/lib,/usr/local/sbin ,/usr/local/share,/usr/local/var,/usr/local/opt,/usr/local/share/zsh,/usr/local/share/zsh/site-functions,/usr/local/var/homebrew,/usr/local/var/homebrew/linked ,/usr/local/Cellar,/usr/local/Caskroom,/usr/local/Homebrew,/usr/local/Frameworks

I removed all the files under /user/local folder, as I knew nothing i have installed there.

Just do this and reinstall homebrew and Enjoy!!

1

None of the other methods I tried worked. I fixed it by doing the following.

  1. download the latest version on https://nodejs.org/en/
  2. after it's finished downloading and you go through all the install steps.
  3. run this command nvm install node --reinstall-packages-from=node
  4. node -v should display latest version
  5. npm install npm@latest -g should also work
0

I got the same problem, here is the solution I got working.

https://stackoverflow.com/a/56962235/563735

0

Solved similar problem with:

sudo mkdir /usr/local/include
sudo chown -R $(whoami) /usr/local/include
brew link node

This could also come in handy for future occasions:

sudo chown -R $(whoami) /usr/local/*
0

If node works in command line but not vscode then try this.. "which node" in command line to check what directory u have it installed in.. .nvm/versions/node/v16.11.0/bin/node and then in vscode u might get the error not not found ... usr/local/bin.. so basically you do this ln -s /Users/.nvm/versions/node/v16.11.0/bin/npm /usr/local/bin/npm. So basically you need to change the directories. But first on is the on dir you get from "which node" in terminal and the second one is the dir from error message.

0

After trying so many of the answers without results. I tried the which node command and it outputted: /Users/myUsername/.nodenv/shims/node This looked odd to me, so I just deleted the .nodenv folder and tried which node and it returned my regular node path. /opt/homebrew/bin/node and every other npm command worked just fine again

Not the answer you're looking for? Browse other questions tagged or ask your own question.