Updating a brew installed npm
I recently tried to update all of my npm installed packages by running
npm -g update and I got this error:
Error: Refusing to delete: /usr/local/bin/npm
Wat. This error shows up when npm tries to update itself.
After a moment of googling for answers, I found this github issue. Yep, I have installed node via brew, and it comes with npm. As it turns out, there’s a problem with npm when it is installed via brew. The official fix is to uninstall the homebrew installed node, download node from the official website, and install that instead. But I love homebrew, so I scrolled down the issue and found an acceptable workaround:
$ npm update -gf $ brew unlink node && brew link --overwrite node
npm -g update makes it a forced update. With that flag, npm will no longer refuse to delete
/usr/local/bin/npm. Well, at least that’s how I understand it. Be careful though, as it’s not a safe thing to do. But I like to live dangerously, so yeah. In case you want to play safe, don’t add the
-f flag on
npm -g update. Instead, only forcefully update those packages that won’t update without the
-f flag. In my case, that’s npm.
The second line is to fix the broken homebrew links. And voila! I now have shiny new things in my global node_modules, without resorting to removing the homebrew installed node.