Create a gist now

Instantly share code, notes, and snippets.

Mac OS X uninstall script for packaged install of node.js

To run this, you can try:

curl -ks https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh | bash

I haven't tested this script doing it this way but i run a lot of my Gists like this so maybe this one'll work too.

Alternatively,

curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
chmod +x ./uninstall-node.sh
./uninstall-node.sh
rm uninstall-node.sh
#!/bin/sh
(( ${#} > 0 )) || {
echo 'DISCLAIMER: USE THIS SCRIPT AT YOUR OWN RISK!'
echo 'THE AUTHOR TAKES NO RESPONSIBILITY FOR THE RESULTS OF THIS SCRIPT.'
echo "Disclaimer aside, this worked for the author, for what that's worth."
echo 'Press Control-C to quit now.'
read
echo 'Re-running the script with sudo.'
echo 'You may be prompted for a password.'
sudo ${0} sudo
exit $?
}
# This will need to be executed as an Admin (maybe just use sudo).
for bom in org.nodejs.node.pkg.bom org.nodejs.pkg.bom; do
receipt=/var/db/receipts/${bom}
[ -e ${receipt} ] && {
# Loop through all the files in the bom.
lsbom -f -l -s -pf ${receipt} \
| while read i; do
# Remove each file listed in the bom.
rm -v /usr/local/${i}
done
}
done
# Remove directories related to node.js.
rm -vrf /usr/local/lib/node \
/usr/local/lib/node_modules \
/var/db/receipts/org.nodejs.*
exit 0
@omarrr

Nice work. I forked to edit line #16 as follows:

 [ -e /var/db/receipts/org.nodejs.pkg.bom ] || {

This looks for the existence of a file (-e) instead of a folder (-d).

Once I did that it worked like a charm. Thanks!

@Bauepete

Thanks.

@mhulse

Great script, thanks! Just FYI to anyone out there new to running shell scripts: I had to run $ chmod u+x uninstall-node.sh and then $ ./uninstall-node.sh before it worked for me. Also, there's a question on Stack related to this code if it is of help to anyone (might be a good place for comments, seeing as how GitHub does not notify anyone (at this time) when comments are posted to Gists).

@ghost
Unknown commented Mar 25, 2013

Had to use mhulse's method to get it to work, thanks though!

@Jurawa

Awesome. Worked great, thanks!

@tezca-antisol

I don't know if is really necessary, but i thing the script should delete ~/.node-gyp y ~/.npm from the local user, maybe even test is there are that directories in other users so you completely remove all what node.js creates

@roman

What is the purpose of (( ${#} > 0 )) ?

@denji

node 0.11.x forgot delete /usr/local/share/systemtap
if empty (/usr/local/share/systemtap + /usr/local/share/systemtap/tapset)

nodejs/node-v0.x-archive#6141

@taoeffect

@roman, it's a hack to skip the first block on the next run.

@ravs

Thanks, nice stuff.

@djdiaz

Thanks! Doesn't work when piped directly from curl to the shell as there is no command line to test in the first block. Might work with "sudo bash" instead, but I didn't bother to hash it out.

@thayneclark

Thanks, worked great. And thanks for the tip mhulse.

@ivancevich

Excellent job. Thanks!

@lrestifo

Worked a treat for me just by saving the script locally and chmod 755 the resulting file

@poliveira89

For now, it seems to be executed successfully! 👍 (For me)

@TanayGahlot

Dint work for me :(

@alexserver

/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file

@nicerobot
Owner

@TanayGahlot Sorry, if you were using the curl method, the URLs were using the old gist URLs so I had to update them to the gist.githubusercontent.com host.
@alexserver That's a curious error. It implies your sudo command doesn't have execution permissions. Maybe run diskutil repairPermissions / ?

@schystz

Weird, I am getting the message "Nothing to do." although org.nodejs.node.pkg.bom is existing in the /var/db/receipts directory. I also tried @omarrr's solution and even executing the script using sudo but that didn't help.

Update:
I tried executing the command lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom from the command line and it works. But if its inside the sh script I get the message can't open /var/db/receipts/org.nodejs.pkg.bom: No such file or directory.

@nicerobot
Owner

@schystz It looks like node's .bom file name may have changed since i originally coded this. I'll update it to look for /var/db/receipts/org.nodejs.node.pkg.bom too.

@serheyShmyg

What about org.nodejs.node.npm.pkg.plist org.nodejs.pkg.plist?
Is it necessary to remove this ones?

@jonathanwoahn

Thank you - this worked perfectly!

@linfan

Nice job!!
Works perfectly for me. Finally removed the nodejs version installed by pkg installer.

@allangagnon

Nice Work!! Thank You!!!

@jaumesalatantinya

Thank you I just run under el capitan and everything seems fine

@ryan-lynch

Seems to freeze terminal for me after echo'ing Press Control-C to quit now.

@youngrrrr

having the same problem as @ryan-lynch. after letting it run for a few minutes, I just killed the program.

@vvardhanz

/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file @nicerobot

@icetee

@vvardhanz use alternative solution.

@Wuntenn

Thanks!! - Great Script!!

@kevin-smets

Running the script by pasting it into a local .sh file worked. Running it through curl did not (at least the first option described here).

However, I wanted to replace my package installed node by a brew install, but I got 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
Target /usr/local/share/doc/node/gdbinit
already exists. You may want to remove it:
  rm '/usr/local/share/doc/node/gdbinit'

Removing it made brew link up node properly. Maybe this should be added to the removals?

(Uninstalled node v4.3.1 and installed v5.6.0 through brew)

@morberg

@ryan-lynch @youngrrrr The script will not start until you press return.

@DonRichards

Thanks!!

@AlexVentura

Thank you I am using NodeJS package from the official website in El Capitan, then I run the script and everything seems fine.

@doncadavona
doncadavona commented Jun 22, 2016 edited

Works like charm!
The alternative worked for me on El Capitan:

curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
chmod +x ./uninstall-node.sh
./uninstall-node.sh
rm uninstall-node.sh
@rsaddey
rsaddey commented Jul 3, 2016 edited

How about virtualising Node.js within Docker in the first place? Right now, Docker appears to be targeted towards builds and production, but IMHO it's a perfect tool to distribute, customise and share developer environments as well. I took a first (quite humble - I'm a newbie to both Docker and Node.js) approach at https://blog.saddey.net/2016/07/03/using-docker-to-create-ionic-2-pwa-developer-environment/ Does this make any sense?

@TheeAndre

@doncadavona's solution worked like a charm.

@FootballFan141

@doncadavona's comment worked! Thanks for this gist! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment