62

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?

Ela
  • 3,142
  • 6
  • 25
  • 40
  • That gist is designed for the pkg installer, not Homebrew. You might try removing node with `brew remove node`, then reinstalling. – Alexander O'Mara Feb 07 '16 at 03: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 Feb 07 '16 at 03: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. – Alexander O'Mara Feb 07 '16 at 03: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 Feb 07 '16 at 03: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. – Alexander O'Mara Feb 07 '16 at 03:29
  • There are too many wrong solution over internet, My advice is try to follow the correct way and not trying anything you find over internet – Pulkit Kumar Singh Aug 03 '19 at 08:13

17 Answers17

79

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.

Ela
  • 3,142
  • 6
  • 25
  • 40
  • 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 – dougmacklin Feb 23 '18 at 18:30
  • 41
    Can't chown /usr/local in OSX High Sierra. Instead, use this `sudo chown -R $(whoami) $(brew --prefix)/*`. See https://github.com/Homebrew/brew/issues/3228 – markhops Mar 16 '18 at 13:00
  • 11
    I needed to execute: brew link --overwrite node – vikzilla May 31 '18 at 03:59
  • 4
    markhops and vikzilla 's comments above were the only thing that worked for me on Mojave 10.14.2 – Greg Hilston Feb 28 '19 at 15:39
  • @markhops & vikzilla fixed it for me as well on Mojave 10.14.6, thanks – Adibe Dec 30 '19 at 21:56
  • @markhops and vikzilla worked on Big Sur 11.1 too. Thank you@ – Darryl Vos Jan 06 '21 at 15:21
  • Operation not permitted. But the answer of desloovere_j helped me – Dmitry Grinko May 29 '21 at 17:33
  • @dougmacklin Do not ```sudo chown -R root /usr/local```. Some files and folders in /usr/local need to be owned by the user instead of root. I did this and now I am getting a lot of errors for not having write permission. – Nate Sep 25 '21 at 11:53
45

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
desloovere_j
  • 716
  • 7
  • 8
22

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

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

brew link --overwrite node
MoD
  • 564
  • 4
  • 14
16

for OSX High Sierra users:

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

brew link --overwrite node

then check node -v and npm -v

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Basavaraj Hadimani
  • 1,094
  • 12
  • 21
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

Debiprasad
  • 5,895
  • 16
  • 67
  • 95
11
sudo chown -R $USER /usr/local
brew link --overwrite node
MoD
  • 564
  • 4
  • 14
NGR
  • 1,230
  • 2
  • 15
  • 44
6

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

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
avivamg
  • 12,197
  • 3
  • 67
  • 61
3

Nothing worked for me except

brew cleanup
Dezigo
  • 3,220
  • 3
  • 31
  • 39
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.

Sylvester Loreto
  • 404
  • 6
  • 18
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
Jonathan.B
  • 29
  • 6
0

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

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

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
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/*
Jose Paez
  • 747
  • 1
  • 11
  • 18
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.

JOHn
  • 63
  • 3
  • 11
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