142

I installed NodeJS from pkg file on my Mac. Now I need to uninstall it. Tell me please how to do it. I tried to remove files from this list:

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom

But node is still on my computer.

Varvara Stepanova
  • 3,489
  • 4
  • 22
  • 26

12 Answers12

177

I ran:

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom \
| while read i; do
  sudo rm /usr/local/${i}
done
sudo rm -rf /usr/local/lib/node \
     /usr/local/lib/node_modules \
     /var/db/receipts/org.nodejs.*

Coded into gist 2697848

Update It seems the receipts .bom file name may have changed so you may need to replace org.nodejs.pkg.bom with org.nodejs.node.pkg.bom in the above. The gist has been updated accordingly.

nicerobot
  • 9,145
  • 6
  • 42
  • 44
  • 3
    Very helpful. Only thing I would add is that node 0.4 ish installed in /usr, not /usr/local. So "sudo rm /usr/local/${i}" needs to be changed. For me I did "sudo rm /${i}" since the bom files where pathed liked: "./usr/bin/node" – Dustin Simpson Mar 16 '12 at 14:04
  • 14
    I created a [gist](https://raw.github.com/gist/2697848/uninstall-node.sh) for it. Use it at your own risk. – nicerobot May 14 '12 at 22:28
  • 6
    @nicerobot Thanks for the gist/script! Probably should point out to others that the actual [gist page for that script is here](https://gist.github.com/nicerobot/2697848). Also, being a n00b to running .sh scripts myself, I had to `$ chmod u+x uninstall-node.sh` and then `$ ./uninstall-node.sh` before it worked for me. :) – mhulse Feb 19 '13 at 18:32
  • @nicerobot and micky-hulse this is most excellent! +1 for both of you. also, try > "which node" or "which npm" after executing the script to see if you still have node/npm installed (or at least, one of the installed versions is gone) – Michahell Mar 08 '13 at 14:37
  • 1
    yea it worked, cool answer. whatever I had a little something left over and just finished that right off with `rm -rf /Users/me/local/bin/node` now rocking node unstable! – Bent Cardan Jun 08 '13 at 10:12
  • make sure you only delete `/var/db/receipts/org.nodejs.*` when every worked successfully. I run the command once, and I had a lot of erreor like `rm: /usr/local/./lib/node_modules/npm/node_modules/which/node_modules/is-absolute/package.json: No such file or directory`. But it was too late, by the end of the script the bom is definitely deleted. Unable to fix and rerun the script – Gomino Aug 19 '19 at 09:43
  • 1
    @Gomino when running things like this, I would put it more generally, you REALLY should check your system and the script to understand if it’s going to do what you want. The script was written against older versions of macOS and Node.js so may require updates. – nicerobot Aug 19 '19 at 13:17
  • 1
    Yeah for sure, I still upvoted your answer by the way – Gomino Aug 19 '19 at 13:39
  • I've also had to run the first command for `/var/db/receipts/org.nodejs.npm.pkg.bom`. – Quartz Feb 01 '21 at 18:08
130

If you installed Node from their website, try this:

sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

This worked for me, but if you have any questions, my GitHub is 'mnafricano'.

Afreekano
  • 1,301
  • 1
  • 8
  • 5
80

Following previous posts, here is the full list I used

sudo npm uninstall npm -g
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d
brew install node
Trefex
  • 2,290
  • 16
  • 18
  • 1
    THANK YOU. My MySQL stopped working so I just needed to remove it, this + a restart did the trick. – Erika Jun 15 '17 at 09:45
  • I also had to remove two additional files: `/usr/local/share/doc/node/gdbinit` and `/usr/local/share/systemtap/tapset/node.stp` – xji Nov 03 '17 at 19:48
  • got 2 errors as rm: /usr/local/bin/node: No such file or directory, rm: /usr/local/share/man/man1/node.1: No such file or directory – vikramvi Jul 22 '19 at 10:12
11

In order to delete the 'native' node.js installation, I have used the method suggested in previous answers sudo npm uninstall npm -g, with additional sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*.

BUT, I had to also delete the following two directories:

sudo rm -rf /usr/local/include/node /Users/$USER/.npm

Only after that I could install node.js with Homebrew.

t0r0X
  • 4,212
  • 1
  • 38
  • 34
  • 3
    was not enough for me - had to also ```sudo rm /usr/local/bin/node /usr/local/share/man/man1/node.1 /usr/local/lib/dtrace/node.d ``` – Hertzel Guinness Jan 11 '15 at 08:13
10

You can use my forked gist: https://gist.github.com/ryangatchalian912/75c6894c3f3143fef366d25eb63437ab

Copy and paste these commands into your Terminal:

curl -ksO https://gist.githubusercontent.com/ryangatchalian912/75c6894c3f3143fef366d25eb63437ab/raw/59c25be64e5555415726bfa824ae41ae1b4539b9/uninstall-node.sh
chmod +x ./uninstall-node.sh
sudo ./uninstall-node.sh > tester.txt
rm uninstall-node.sh

It works on Mac OSX Big Sur (11.4+).

Ryan Gatchalian
  • 111
  • 1
  • 3
8

This is the full list of commands I used (Many thanks to the posters above):

sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
brew install node
AhrB
  • 111
  • 1
  • 3
6

Use npm to uninstall. Just running sudo npm uninstall npm -g removes all the files. To get rid of the extraneous stuff like bash pathnames run this (from nicerobot's answer):

sudo rm -rf /usr/local/lib/node \ /usr/local/lib/node_modules \ /var/db/receipts/org.nodejs.*

alexbhandari
  • 1,310
  • 12
  • 21
5

I took AhrB's list, while appended three more files. Here is the full list I have used:

sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/bin/npm
sudo rm /usr/local/share/systemtap/tapset/node.stp
sudo rm /usr/local/lib/dtrace/node.d
# In case you want to reinstall node with HomeBrew:
# brew install node
hailong
  • 1,409
  • 16
  • 19
3

A little convenience script expanding on previous answers.

#!/bin/bash

# Uninstall node.js
# 
# Options:
#
# -d Actually delete files, otherwise the script just _prints_ a command to delete.
# -p Installation prefix. Default /usr/local
# -f BOM file. Default /var/db/receipts/org.nodejs.pkg.bom

CMD="echo sudo rm -fr"
BOM_FILE="/var/db/receipts/org.nodejs.pkg.bom"
PREFIX="/usr/local"

while getopts "dp:f:" arg; do
    case $arg in
        d)
            CMD="sudo rm -fr"
            ;;
        p)
            PREFIX=$arg
            ;;
        f)
            BOM_FILE=$arg
            ;;
    esac
done

lsbom -f -l -s -pf ${BOM_FILE} \
    | while read i; do
          $CMD ${PREFIX}/${i}
      done

$CMD ${PREFIX}/lib/node \
     ${PREFIX}/lib/node_modules \
     ${BOM_FILE}

Save it to file and run with:

# bash filename.sh
1

A decent way to uninstall node installed from pkg

See what is in db/receipts

ls /var/db/receipts/org.node*

Peek into the relevant bom files noting the paths listed

  • lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom
  • lsbom -f -l -s -pf /var/db/receipts/org.nodejs.npm.pkg.bom

Do the deletes. Use prefix on remove depending on the paths above.

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do sudo rm /${i}; done

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.npm.pkg.bom | while read i; do sudo rm /${i}; done

Remove the remaining folders

  • sudo rm -rf /usr/local/lib/node_modules
  • sudo rm -rf /var/db/receipts/org.nodejs.*

Check it is really gone

  • which node
  • which npm
Rohanthewiz
  • 947
  • 9
  • 9
0

I had to remove the following files too since brew complained in install later after manually removing all files.

/usr/local/share/doc/node/gdbinit

/usr/local/share/systemtap/tapset/node.stp

and then do the following

brew install node 

brew link node
mhasan
  • 3,703
  • 1
  • 18
  • 37
Srini 7
  • 1
  • 1
0

The following worked after trial and error, and these directories were not writable so, I removed them and finally was able to get node & npm replaced.

sudo rm -rf /usr/local/share/systemtap
sudo rm -rf /usr/local/share/doc/node
sudo rm -rf /usr/local/Cellar/node/9.11.1
brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-9.11.1.high_sierra.bottle.tar.gz
Already downloaded: /Users/xxx/Library/Caches/Homebrew/node-9.11.1.high_sierra.bottle.tar.gz
==> Pouring node-9.11.1.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
  /usr/local/Cellar/node/9.11.1: 5,125 files, 49.7MB

node -v
v9.11.1
npm -v
5.6.0
Echo Nolan
  • 1,111
  • 1
  • 11
  • 23
  • 1
    I modified your formatting. You can select a block of text and click the curly brace icon to get monospace text. Or indent every line with four spaces if you're a masochist. – Echo Nolan Apr 07 '18 at 17:36