Skip to content

Instantly share code, notes, and snippets.

@psyrendust
Last active May 8, 2019 18:27
Show Gist options
  • Save psyrendust/9c6c5c9b09eac1eab0c9 to your computer and use it in GitHub Desktop.
Save psyrendust/9c6c5c9b09eac1eab0c9 to your computer and use it in GitHub Desktop.
Remove Node and NPM from OS X and Linux
#!/usr/bin/env bash
#
# remove-node.sh
#
# Find and remove all traces of Node and NPM from OS X and Linux.
# Log all file deletions to ~/remove_node_YYYY_MM_DD_HH_MM_SS.log
#
# Author:
# Larry Gordon
#
# License:
# The MIT License (MIT) <http://psyrendust.mit-license.org/2014/license.html>
# ------------------------------------------------------------------------------
# Create log filename
logfile="$HOME/remove_node_$(date +%Y_%m_%d_%H_%M_%S).log"
node_files=()
node_folders=()
node_receipts=()
echo "Check if Node was installed via Homebrew" >> $logfile
if [[ -n $(which brew | grep "/brew$") ]] && [[ -n "$(brew list 2>/dev/null | grep "^node$")" ]]; then
echo " - [info] Node installed via Homebrew" >> $logfile
brew uninstall node && echo " - [ok] brew uninstall node" >> $logfile
brew cleanup
else
echo " - [info] Node not installed via Homebrew" >> $logfile
fi
echo "" >> $logfile
echo "Check if Node was installed via 1-click installer" >> $logfile
if [[ -a /var/db/receipts/org.nodejs.pkg.bom ]]; then
echo " - [info] Node installed via 1-click installer" >> $logfile
echo " - [info] Find all Node and NPM files via /var/db/receipts/org.nodejs.pkg.bom" >> $logfile
node_files+=( $(lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom | while read f; do echo "/usr/local/${f}"; done) )
if [[ -d /var/db/receipts ]]; then
echo " - [info] Find all Node and NPM receipts" >> $logfile
node_receipts+=( $(find /var/db/receipts -name "org.nodejs.*") )
fi
else
echo " - [info] Node not installed via 1-click installer" >> $logfile
fi
echo "" >> $logfile
echo "Search for remaining files" >> $logfile
echo " - [info] Find all Node and NPM files" >> $logfile
[[ -d /usr/local/bin ]] && node_files+=( $(find /usr/local/bin -name "npm" -maxdepth 1) )
[[ -d /usr/local/bin ]] && node_files+=( $(find /usr/local/bin -name "node" -maxdepth 1) )
[[ -d /usr/local/lib ]] && node_files+=( $(find /usr/local/lib -name "npm" -maxdepth 1) )
[[ -d /usr/local/lib ]] && node_files+=( $(find /usr/local/lib -name "node" -maxdepth 1) )
[[ -d /opt/local/bin ]] && node_files+=( $(find /opt/local/bin -name "npm" -maxdepth 1) )
[[ -d /opt/local/bin ]] && node_files+=( $(find /opt/local/bin -name "node" -maxdepth 1) )
[[ -d /opt/local/lib ]] && node_files+=( $(find /opt/local/lib -name "npm" -maxdepth 1) )
[[ -d /opt/local/lib ]] && node_files+=( $(find /opt/local/lib -name "node" -maxdepth 1) )
[[ -d /usr/local/share/man ]] && node_files+=( $(find /usr/local/share/man -name "npm*") )
[[ -d /usr/local/share/man ]] && node_files+=( $(find /usr/local/share/man -name "node*") )
[[ -d /usr/local/lib/dtrace ]] && node_files+=( $(find /usr/local/lib/dtrace -name "node*") )
echo " - [info] Find all Node and NPM folders" >> $logfile
[[ -d /usr/local ]] && node_folders+=( $(find /usr/local -name "node_modules" -maxdepth 2) )
[[ -d /opt/local ]] && node_folders+=( $(find /opt/local -name "node_modules" -maxdepth 2) )
[[ -d ~/.npm ]] && node_folders+=( $(find $HOME -name ".npm" -maxdepth 1) )
[[ -d ~/.node-gyp ]] && node_folders+=( $(find $HOME -name ".node-gyp" -maxdepth 1) )
echo "" >> $logfile
echo "Remove all Node and NPM files" >> $logfile
[[ ${#node_files[@]} > 0 ]] || echo " - [info] No files found" >> $logfile
for node_file in ${node_files[@]}; do
[[ -a "$node_file" ]] && sudo rm -rf "$node_file" && echo " - [ok] Removed: $node_file" >> $logfile
done
unset node_file{,s}
echo "" >> $logfile
echo "Remove all Node and NPM folders" >> $logfile
[[ ${#node_folders[@]} > 0 ]] || echo " - [info] No folders found" >> $logfile
for node_folder in ${node_folders[@]}; do
[[ -a "$node_folder" ]] && sudo rm -rf "$node_folder" && echo " - [ok] Removed: $node_folder" >> $logfile
done
unset node_folder{,s}
echo "" >> $logfile
echo "Remove all Node and NPM receipts" >> $logfile
[[ ${#node_receipts[@]} > 0 ]] || echo " - [info] No receipts found" >> $logfile
for node_receipt in ${node_receipts[@]}; do
[[ -a "$node_receipt" ]] && sudo rm -rf "$node_receipt" && echo " - [ok] Removed: $node_receipt" >> $logfile
done
unset node_receipt{,s}
echo "Check if Node was installed via NVM" >> $logfile
if [[ -a "$HOME/.nvm" ]]; then
echo " - [info] Node installed via NVM" >> $logfile
if [[ -n $(which nvm | grep "/nvm$") ]]; then
nvm deactivate && echo " - [ok] nvm deactivate" >> $logfile
fi
rm -rf "$HOME/.nvm" && echo " - [ok] Removed: $HOME/.nvm" >> $logfile
else
echo " - [info] Node not installed via NVM" >> $logfile
fi
printf '\033[0;32m%s\033[0m\n' "[complete]"
printf '\033[0;32m%s\033[0m\n' "Don't forget to clean up any references to Node, NPM, or NVM in your ~/.bash_profile, ~/.bashrc, or ~/.profile"
@psyrendust
Copy link
Author

Usage

Using cURL:

curl -sSL https://gist.githubusercontent.com/psyrendust/9c6c5c9b09eac1eab0c9/raw/e9301214c6159ed122db3b74647f05402010797a/remove-node.sh | bash

Using Wget:

wget -qO- https://gist.githubusercontent.com/psyrendust/9c6c5c9b09eac1eab0c9/raw/e9301214c6159ed122db3b74647f05402010797a/remove-node.sh | bash

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