Skip to content

Instantly share code, notes, and snippets.

@ryangatchalian912
Forked from omarrr/uninstall-node.sh
Last active July 13, 2023 02:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryangatchalian912/75c6894c3f3143fef366d25eb63437ab to your computer and use it in GitHub Desktop.
Save ryangatchalian912/75c6894c3f3143fef366d25eb63437ab to your computer and use it in GitHub Desktop.
Mac OS X uninstall script for packaged install of Node.js
#!/bin/bash
(( ${#} >= 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).
# Verify that Node.js bom exists. Otherwise, don't do anything.
[ -e /var/db/receipts/org.nodejs.node.pkg.bom ] || {
echo '>>> Node.js not found. Nothing to do.'
exit 0
}
# Verify that the npm bom exists. Otherwise, don't do anything.
[ -e /var/db/receipts/org.nodejs.npm.pkg.bom ] || {
echo '>>> npm not found. Nothing to do.'
exit 0
}
for bom in org.nodejs.node.pkg.bom org.nodejs.npm.pkg.bom; do
receipt=/var/db/receipts/${bom}
echo ">>> Removing bom files from ${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#./usr/local/}
done
}
done
# Remove files and directories related to Node.js and npm.
echo '>>> Removing Node.js and npm files and directories...'
sudo rm -rfv /usr/local/lib/node_modules \
/usr/local/lib/node \
/usr/local/include/node \
/usr/local/share/doc/node \
/usr/local/share/man/{man1,man5}/npm* \
/usr/local/share/man/{man1,man5}/npx* \
/usr/local/share/man/{man1,man5}/install* \
/usr/local/share/man/{man1,man5}/folders* \
/usr/local/share/man/{man1,man5}/package-json* \
/usr/local/share/man/{man1,man5}/package-lock* \
/usr/local/share/man/{man1,man5}/shrinkwrap* \
/usr/local/share/man/man7 \
/usr/local/bin/{npx,npm,node} \
/var/db/receipts/org.nodejs.* \
~/.node-gyp \
~/.npm
exit 0
@rogfut
Copy link

rogfut commented Oct 30, 2021

Works great, M1 macOS Monterey 12.0.1

@Genkilabs
Copy link

Work on 12.3 M1 Pro. Thank you

@danielerhabor
Copy link

Works on macOS Ventura 13.1, M1 Macbook Air 2020

@Nusnus
Copy link

Nusnus commented Mar 9, 2023

Works on macOS Monterey (12.6.3), M1 Max.
Thanks!

@harshmandan
Copy link

Works on M1 Pro Mac, Monterey!

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