Cannot update NPM: Refusing the delete…Error

I just want to quickly put the solution I have found here since I spent a couple of hours fixing this issue last night. Because @Angular/Cli package requires a higher than 5.x version of NPM, I had to run the npm install -g npm to update the NPM (Node Package Manager). I was having lots of checkPermission issues. When I run the same command as su by using sudo command as seen here: sudo npm install -g npm I was having the following issue:

refusing to delete /usr/local/share/man/man7/semver.7: ../../../../../private/tmp/npm.732/package/man/man7/semver.7 symlink target is not controlled by npm /usr/local/share/man/man7

The problem is the fact that I had Homebrew installed on my machine. I probably attempted to install node using Homebrew at some point of time and now it was biting me somehow.

The Fix

The fix is simple. Just run brew prune which will remove bunch of symbolic links from node locations. Later, in case you want to be on the safe side, run brew doctor . In my case, it was not required but I ran it while I was figuring out the problem. However, I didn’t have to run after the brew prune . Anyways, once the pruning operation is complete, go ahead and run sudo npm install -g npm again and see if that helps.

Links

Some of the places where similar problems reported that I have read to figure out what’s going on:

  1. https://github.com/npm/npm/issues/20172
  2. https://github.com/coreybutler/nvm-windows/issues/300
  3. https://gist.github.com/DanHerbert/9520689 — This link may provide some in-depth explanation about what might be going on with the homebrew and npm.

They didn’t provide a working solution to me but helped me with understanding that the issue might have been with the Homebrew package manager I installed years ago and still suffering from that decision of mine.