Skip to content

Instantly share code, notes, and snippets.

@shaneholloman
Created October 23, 2024 00:00
Show Gist options
  • Save shaneholloman/d773d0c0d0fff9d5b0d3dabf659a9980 to your computer and use it in GitHub Desktop.
Save shaneholloman/d773d0c0d0fff9d5b0d3dabf659a9980 to your computer and use it in GitHub Desktop.
complete-node-cleanup
#!/bin/bash
echo "Starting Node.js environment discovery..."
# Create a log file
LOG_FILE="node-cleanup-$(date +%Y%m%d-%H%M%S).log"
exec 1> >(tee -a "$LOG_FILE") 2>&1
# Discovery Phase
echo "=== Current Environment State ==="
echo "Node executables found at:"
which node npm nvm volta n nodenv 2>/dev/null || echo "No Node executables found"
if command -v node &> /dev/null; then
echo "Node version: $(node -v)"
fi
if command -v npm &> /dev/null; then
echo "NPM version: $(npm -v)"
echo "Global NPM packages:"
npm list -g --depth=0
fi
echo "System Node packages:"
dpkg -l | grep -i node
# Backup Phase
echo "=== Creating Backups ==="
BACKUP_DIR="node-backup-$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
if command -v npm &> /dev/null; then
echo "Backing up global npm packages list..."
npm list -g --depth=0 > "$BACKUP_DIR/npm-global-packages.txt"
fi
# Configuration backup
for file in ~/.npmrc ~/.nvm/nvm.sh ~/.nodenv/version; do
if [ -f "$file" ]; then
echo "Backing up $file..."
cp "$file" "$BACKUP_DIR/$(basename $file)"
fi
done
echo "=== Starting Cleanup ==="
# Remove apt packages
if dpkg -l | grep -i nodejs > /dev/null; then
echo "Removing Node.js apt packages..."
sudo apt-get remove --purge nodejs nodejs-doc npm -y
sudo apt-get autoremove -y
fi
# Clean npm if it exists
if command -v npm &> /dev/null; then
echo "Cleaning npm cache..."
npm cache clean --force
fi
# Remove version managers
if [ -d "$HOME/.nvm" ]; then
echo "Removing NVM..."
nvm deactivate && nvm unload 2>/dev/null || true
rm -rf "$HOME/.nvm"
fi
if command -v n &> /dev/null; then
echo "Removing n version manager..."
n uninstall
sudo rm -rf /usr/local/n
sudo rm -rf /usr/local/bin/n
fi
if [ -d "$HOME/.nodenv" ]; then
echo "Removing nodenv..."
rm -rf "$HOME/.nodenv"
fi
# Remove Node-related directories and files
echo "Removing Node.js related directories and files..."
sudo rm -rf /usr/local/bin/npm \
/usr/local/share/man/man1/node* \
/usr/local/lib/dtrace/node.d \
~/.npm \
~/.node-gyp \
/usr/local/lib/node_modules \
/usr/local/include/node \
/usr/local/bin/node
# Clean shell configs
echo "Cleaning shell configuration files..."
for config in ~/.bashrc ~/.zshrc ~/.bash_profile ~/.zprofile; do
if [ -f "$config" ]; then
echo "Cleaning $config..."
sed -i '/NVM_DIR/d' "$config"
sed -i '/nvm.sh/d' "$config"
sed -i '/bash_completion/d' "$config"
sed -i '/nodenv/d' "$config"
sed -i '/volta/d' "$config"
fi
done
echo "=== Verification Phase ==="
echo "Checking for remaining Node.js installations..."
which node npm nvm volta n nodenv 2>/dev/null || echo "No Node executables found"
[ -d "$HOME/.nvm" ] || echo "NVM is removed"
[ -d "$HOME/.nodenv" ] || echo "nodenv is removed"
[ -f "/usr/local/bin/n" ] || echo "n is removed"
echo "=== Cleanup Complete ==="
echo "Backup directory: $BACKUP_DIR"
echo "Log file: $LOG_FILE"
echo "You can now proceed with Volta installation"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment