188

I'm trying to install node.js via Homebrew. Unfortunately, I get this error:

➜  ~  brew install node
==> Downloading http://nodejs.org/dist/v0.8.10/node-v0.8.10.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/node/0.8.10
==> make install
==> Caveats
Homebrew installed npm.
We recommend prepending the following path to your PATH environment
variable to have npm-installed binaries picked up:
  /usr/local/share/npm/bin
Warning: Could not link node. Unlinking...
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link node'
==> Summary
/usr/local/Cellar/node/0.8.10: 856 files, 13M, built in 103 seconds

So then I try to link node manually...

➜  ~  brew link node
Linking /usr/local/Cellar/node/0.8.10... Warning: Could not link node. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/node/0.8.10/lib/node_modules/npm/scripts/relocate.sh
Target /usr/local/lib/node_modules/npm/scripts/relocate.sh already exists. You may need to delete it.
To force the link and delete this file, do:
  brew link -f formula_name

To list all files that would be deleted:
  brew link -n formula_name

No luck, so I try forcefully linking node...

➜  ~  brew link -f node
Linking /usr/local/Cellar/node/0.8.10... Warning: Could not link node. Unlinking...

Error: Permission denied - /usr/local/lib/dtrace/node.d

I'm not familiar with dtrace in any regard and I'm afraid to mess with permissions, so I tried to sudo..

➜ ~ git:(master) sudo brew link -f node
Password:
Error: Cowardly refusing to `sudo brew link'
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.

I've tried uninstalling node via brew uninstall node and retrying the install with no luck.

chandlervdw
  • 3,197
  • 4
  • 19
  • 21

22 Answers22

167

I run Mac OS X Mavericks. I tried to install node 0.10.25 and the top answer did not work for me.

natevw says to rm -rf /usr/local/lib/node_modules/npm but if the permissions on /usr/local/lib/node_modules look like this:

drwxr-xr-x   3 root      admin   102 Feb  2 20:45 node_modules

then brew will not be able to create its npm symlink in that directory. Here's my solution:

Step 1: Update Homebrew

$ brew update

Step 2: Remove node/npm everywhere on your system

Some of these commands are not necessary depending on how you installed node/npm in the past.

$ brew uninstall npm
$ brew uninstall node
$ npm uninstall npm -g
$ sudo rm -rf /usr/local/lib/node_modules

Note: I had stray node files that I found by running brew -v link node (which gave me the verbose output of the linking errors brew was complaining about). You may need to:

$ sudo rm -rf /usr/local/include/node
$ sudo rm -rf /usr/local/lib/node

Step 3: Open a new terminal and install node

$ brew install node
Jim Geurts
  • 20,189
  • 23
  • 95
  • 116
franksort
  • 3,093
  • 2
  • 19
  • 27
146

The homebrew package for node.js now includes npm again, so this happened to me when I missed the homebrew package's message about removing the standalone version first.

Assuming, like me, you've already broken node/npm by attempting the upgrade before knowing to npm uninstall npm -g first, you can rm -rf /usr/local/lib/node_modules/npm and then brew link node. This removes the standalone self-hosted npm package (rather than the one brew would like to install) and lets brew symlink its bundled one from Cellar.

natevw
  • 16,807
  • 8
  • 66
  • 90
  • 11
    After you remove npm you need to close and re-open your terminal to remove it from your path in order for linking to work properly, as @dain mentioned in his comment. – kflorence Aug 21 '13 at 00:19
  • This worked for me. In my case, I couldn't link Elixir and Erlang during an upgrade. The directories of concern were not owned by my username, but owned by "root". I decided to delete those directories and reinstall. – Coach Roebuck Mar 12 '14 at 20:24
  • 1
    when I try link node I get: **Error: No such keg: /usr/local/Cellar/node *|* any ideas? – SuperUberDuper Feb 09 '15 at 17:12
  • 1
    After trying to upgrade npm, I spent an hour trying to figure this out. Brew would not let me link until the standalone version was removed first. Recommendations from nate worked for me. – Brandon Clapp Aug 12 '15 at 00:58
  • How do i reinstall npm? Or does it get installed with brew install node? – marciokoko Apr 02 '16 at 16:44
  • @marciokoko It should get installed via `brew install node`, only for some time years ago did brew leave npm out afaik. – natevw Apr 04 '16 at 20:06
  • @natevw Thanks. I think what happened was a version conflict or install conflict. – marciokoko Apr 04 '16 at 23:17
  • Linking /usr/local/Cellar/node/10.0.0... `Error: Could not symlink include/node/openssl/archs/BSD-x86_64/asm/crypto/buildinf.h /usr/local/include/node/openssl/archs/BSD-x86_64/asm/crypto is not writable` I tried, but failed – ValRob May 08 '18 at 16:51
43
sudo chown -R $(whoami) /usr/local 

would do just fine as mentioned in the brew site troubleshooting

https://github.com/Homebrew/homebrew/wiki/troubleshooting

Talal
  • 703
  • 5
  • 11
  • 3
    I'm surprised that they recommend this. Wouldn't it be better to do: `sudo chmod -R g+w /usr/local` (although honestly, both scare me) – getWeberForStackExchange Oct 14 '14 at 05:56
  • @getWeberForStackExchange I tried what you said but it didn't make a difference for me. Any ideas why that might be? – f1lt3r Jan 20 '16 at 18:48
  • @AlistairMacDonald You've probably worked this out by now, but I'd probably need more info to work out why you were having trouble. Changing the ownership of everything in /usr/local is supposed to allow the current user to create the node links in the /usr/local structure. I was proposing to just change the group permissions of every file instead of ownership, which is slightly less crazy. My command assumed that your user is part of the group that owns all of /usr/local. It's also possible that there are different groups in /usr/local, which would require a `chgrp` command. – getWeberForStackExchange Feb 15 '16 at 22:49
  • I installed home-brew under OS X 10.11 specifically to get midnight commander and wget. I do not have npm installed. The "brew link" step failed as described in the question during the mc install. This answer worked for me; I made the change, re-ran the link with "brew link midnight-commander", which then ran fine. After that, typing "mc" at the command line brought up midnight commander as expected. After that, "brew install wget" worked with no fanfare. Great answer. – fyngyrz May 26 '16 at 15:24
  • this command is recommended by `brew doctor`, but I prefer `sudo su`, execute the commands then close the terminal – addlistener Oct 23 '16 at 01:40
  • Note that this is no longer possible on MacOS. No one, not even root can `chown` or `chgrp` `/usr` or anything under it. – bluesmoon May 30 '18 at 13:42
  • 1
    `chown: /usr/local: Operation not permitted ` ¯\_(ツ)_/¯ – mbdavis Jun 14 '19 at 10:20
39

You probably already installed an older version of node.js using a different method, so you need to manually remove the files that are getting in brew's way.

Do brew link -n node and manually delete those conflicting files and directories, then try brew link node again.

Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
33

I'm a bit late, what worked for me was this:

* npm uninstall npm -g

* brew uninstall node

* brew install node

* sudo rm -rf /usr/local/lib/dtrace/node.d

* brew link node (caused error with permissions)

* sudo chmod 777 /usr/local/lib/dtrace/node.d

* brew link node

Everything was successful after this sequence

0bserver07
  • 3,390
  • 1
  • 28
  • 56
Will
  • 664
  • 7
  • 23
  • This finally worked for me. I was getting issues after i tried to install node via brew after already having npm installed. It essentially broke npm for me. I had to rm -rf the npm folder then install via brew (npm uninstall no longer worked for me). After that the rest of the steps worked perfectly. – rdrobinson3 Dec 20 '13 at 14:36
  • 5
    `sudo chmod 777` This is never the right solution, if you're having permissions issues, investigate why they are occurring and set the minimum level of permissions required to do the job. This command will allow the file to be modified by anyone on the system. – bobmagoo Oct 29 '14 at 05:01
  • Brilliant. Thank you. Although I had no perms issues running `brew link node` for the first time. – Bede Constantinides Feb 19 '15 at 18:35
  • Rather than chmod that directory I just took ownership of it. chown myuser -R -v /usr/local/lib/dtrace/ I also had the problem that dtrace didn't exist as a directory. The error was the same, but no directory was present. – Jason Short Feb 02 '16 at 23:42
27

Try this. Got from another reference and worked for me.

brew uninstall node
brew update
brew upgrade
brew cleanup
brew install node
sudo chown -R $(whoami) /usr/local
brew link --overwrite node
brew postinstall node
Dani Amsalem
  • 1,266
  • 17
  • 26
Randula
  • 1,537
  • 15
  • 11
  • 2
    `brew postinstall node` was the ticket – prototype Jun 06 '17 at 03:17
  • 1
    This was the ticket! For some reason, I had to run brew upgrade 2X and on the second time it asked me to install an update to XCode. Between `brew link --overwrite node` and the final command, it all seems to work. The final command you suggested doesn't work because of 'sudo. Instead, use `brew postinstall node` – Dani Amsalem Mar 11 '20 at 03:55
10

I had the same problem after transferring all my applications from my old Mac to my new one.

I found the solution by running brew doctor :

Warning: Broken symlinks were found. Remove them with brew prune

After running brew prune, Homebrew is finally back on track :)

Kévin L. R.
  • 101
  • 1
  • 3
4

My problem had a slightly different solution. The directory in which brew wanted to create the symlinks were not owned by the current user.

ls -la /usr/local/bin/lib/node | grep node yielded:

drwxr-xr-x    3 24561  wheel   102 May  4  2012 node
drwxr-xr-x    7 24561  wheel   238 Sep 18 16:37 node_modules

For me, the following fixed it:

sudo chown $(users) /usr/local/bin/lib/node_modules
sudo chown $(users) /usr/local/bin/lib/node

ps. $(users) will get expanded to your username, went a little out of my way to help out lazy copy pasters ;)

suweller
  • 514
  • 3
  • 13
  • 2
    I had the same issue. This can apparently occur if you run "make install" for some other app manually. I had recently done just that and the solution was to chown like you did. – Richard Tuttle Feb 22 '14 at 11:47
4

thx @suweller.

I fixed the problem:

➜  bin git:(master) ✗ brew link node
Linking /usr/local/Cellar/node/0.10.25... Warning: Could not link node. Unlinking...

Error: Permission denied - /usr/local/lib/node_modules/npm

I had the same problem as suweller:

➜  bin git:(master) ✗ ls -la /usr/local/lib/ | grep node
drwxr-xr-x   3 24561  wheel   102 11 Okt  2012 node
drwxr-xr-x   3 24561  wheel   102 27 Jan 11:32 node_modules

so i fixed this problem by:

➜  bin git:(master) ✗ sudo chown $(users) /usr/local/lib/node_modules
➜  bin git:(master) ✗ sudo chown $(users) /usr/local/lib/node

after i fixed this problem I got another one:

➜  bin git:(master) ✗ brew link node
Linking /usr/local/Cellar/node/0.10.25... Warning: Could not link node. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/node/0.10.25/lib/dtrace/node.d
Target /usr/local/lib/dtrace/node.d already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
  brew link --overwrite formula_name

To list all files that would be deleted:
  brew link --overwrite --dry-run formula_name

So I removed node.d by:

➜  bin git:(master) ✗ sudo rm /usr/local/lib/dtrace/node.d

got another permission error:

➜  bin git:(master) ✗ brew link node
Linking /usr/local/Cellar/node/0.10.25... Warning: Could not link node. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/node/0.10.25/lib/dtrace/node.d
/usr/local/lib/dtrace is not writable. You should change its permissions.

and fixed it:

➜  bin git:(master) ✗ sudo chown $(users) /usr/local/Cellar/node/0.10.25/lib/dtrace/node.d

and finally everything worked:

➜  bin git:(master) ✗ brew link node
Linking /usr/local/Cellar/node/0.10.25... 1225 symlinks created
zero3nna
  • 2,770
  • 30
  • 28
3

the ultimate answer: change the owner of that directory to whoever you are

sudo chown -R `whoami` /usr/local/include

which is also recommended by brew if you run brew doctor

nobody
  • 19,814
  • 17
  • 56
  • 77
Bobo
  • 8,777
  • 18
  • 66
  • 85
3

Most brew install issues with node are caused by permission errors or having node previously installed and then trying to install it via brew. The solution that worked for me finally was:

WARNING: This will uninstall nodejs (multiple versions) use with caution:

  1. Remove node via brew:

    brew uninstall node

  2. also did via force:

    brew uninstall node --force

  3. To use the script Source: Remove node:

    curl -O https://raw.githubusercontent.com/DomT4/scripts/master/OSX_Node_Removal/terminatenode.sh

Then:

chmod +x /path/to/terminatenode.sh

Then:

./terminatenode.sh .
  1. Then make sure to do the following command:

    chown $USER /usr/local

  2. Then do a brew update (keep doing this until all things are updated):

    brew update

  3. Clean brew up and run update again (might be redundant) and run doctor to make sure things are in place:

    brew cleanup; brew update; brew doctor

  4. And finally install node via brew (verbose):

    brew install -v node

AhsenBaig
  • 497
  • 4
  • 21
  • The numbers aren't formatting correctly... but the steps are there. Will correct the formatting issue tomorrow. – AhsenBaig Aug 25 '14 at 05:15
2

I was getting an error while installation that not able to link node and /usr/local/include is not writable

Below solution worked for me :- First create the include folder, note that this requires sudo privileges

cd /usr/local

sudo mkdir include

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

brew link node
Pukhraj soni
  • 1,832
  • 2
  • 17
  • 11
1

I also managed to mess up my NPM and installed packages between these Homebrew versions and no matter how many time I unlinked / linked and uninstalled / installed node it still didn't work.

As it turns out you have to remove NPM from the path otherwise Homebrew won't install it: https://github.com/mxcl/homebrew/blob/master/Library/Formula/node.rb#L117

Hope this will help someone with the same problem and save that hour or so I had to spend looking for the problem...

dain
  • 6,475
  • 1
  • 38
  • 47
1

by the Finder, Delete this file:

/usr/local/lib/dtrace/node.d

in terminal:

$ brew link --overwrite --dry-run node

then:

$ brew link node
Hiddenmine
  • 11
  • 2
  • 2
    When I run `brew link --overwrite --dry-run node`, I see the error `No such keg: /usr/local/Cellar/node`. What am I missing? – PKHunter May 22 '18 at 17:23
1

I have tried all of the methods and none of them works. The easiest way is to reinstall node from https://nodejs.org/en/download/

Simply download the pkg and install it. Now I have a working npm and node again.

Hunter
  • 3,080
  • 20
  • 23
0

Don't know, if it's a good idea or not: After trying all other solutions without success, I just renamed /usr/local/lib/dtrace, linked node and re-renamed the directory again. After that, node worked as expected.

Alex Maiburg
  • 690
  • 5
  • 10
0

I was struggling with this for a while. (for me "npm uninstall npm -g" did nothing)

I tried a bunch of things:

npm uninstall npm -g
brew uninstall node
brew install node

I was still having some issues and was getting errors when i tried to link the node files

Finally i tried this

brew link --overwrite node

That seemed to have fixed it. (it overwrites al the conflicting files and also links node and npm)

Hope this helps

T J
  • 659
  • 1
  • 8
  • 18
0

I completely uninstalled brew and started again, only to find the same problem again.

Brew appears to work by symlinking the required binaries into your system where other installation methods would typically copy the files.

I found an existing set of node libraries here:

/usr/local/include/node

After some head scratching I remembered installing node at the date against this old version and it hadn't been via brew.

I manually deleted this entire folder and successfully linked npm.

This would explain why using brew uninstall or even uninstall brew itself had no effect.

The highest ranked answer puts this very simply, but I thought I'd add my observations about why it's necessary.

I'm guessing a bunch of issues with other brew packages might be caused by old non-brew instances of packages being in the way.

Henry
  • 7,721
  • 2
  • 38
  • 38
0

Had been wrecking my head on symlinking node .. and nothing seemed to work...but finally what worked is setting the right permissions . This 'sudo chown -R $(whoami) /usr/local' did the work for me.

Shahsayyad
  • 39
  • 9
0

I fixed this in El Capitan by using the following command. Honestly, no idea what it does, but thought I'd share since it fixed my problem.

brew link --overwrite node
Fizzix
  • 23,679
  • 38
  • 110
  • 176
0

In summary, I had some left over files in /usr/local/share/man/man1 which were node.1 and npm*, * being a wild card for 10s of files.

usr/local/share/man/man1. Directory

I used:

rm npm*
rm node.1

further to the commands demonstrated by https://stackoverflow.com/a/21520777/7963882.

Then brew install node worked successfully, installing both node node 20.5.1 and npm 9.8.0.

Scotty
  • 11
  • 3
-5

You need to remove the npm package manually.

first unlink node: brew link -n node

remove npm folder: rm -R /usr/local/Cellar/node/0.8.10/lib/node_modules/npm

link again: brew link node

there will be a soft link to the new location of npm

soenke
  • 1,023
  • 1
  • 8
  • 8
  • 5
    Doesn't this remove the NEW npm that brew is trying to install? Following this left me without a working npm installed, whereas deleting the OLD npm folder as in my answer works. – natevw Oct 05 '12 at 17:56
  • it looks to me like you're trying to remove the new package too - whereas surely it's the old one that needs removing? I actually found that there was a symlink left over for /usr/local/bin/npm – Caroline Mar 12 '13 at 15:56