55
votes

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?

15
That gist is designed for the pkg installer, not Homebrew. You might try removing node with brew remove node, then reinstalling. - Alexander O'Mara
@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
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. - Alexander O'Mara
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
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. - Alexander O'Mara

15 Answers

65
votes

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.

34
votes

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
13
votes

for OSX High Sierra users:

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

brew link --overwrite node

then check node -v and npm -v

12
votes

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
votes
sudo chown -R $USER /usr/local
brew link --overwrite node
9
votes

For Mojave || Catalina || Big Sur use:

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

brew link --overwrite node
6
votes

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

4
votes

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.

2
votes

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
votes

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
1
votes

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

0
votes

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

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

0
votes

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
votes

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
votes

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.