Skip to content

Instantly share code, notes, and snippets.

@fuzzbuster
Created September 9, 2025 02:58
Show Gist options
  • Select an option

  • Save fuzzbuster/388b4a93b0c01268385e16140496ab17 to your computer and use it in GitHub Desktop.

Select an option

Save fuzzbuster/388b4a93b0c01268385e16140496ab17 to your computer and use it in GitHub Desktop.
This script will ensure all your global CLI tools are updated alongside Node.js and npm, minimizing compatibility issues after the version upgrade.
#!/bin/bash
set -e
# Color output functions
info() { echo -e "\033[1;34m$1\033[0m"; }
success() { echo -e "\033[1;32m$1\033[0m"; }
error() { echo -e "\033[1;31m$1\033[0m"; exit 1; }
# --------------------------
# Detect nvm installation (optimized for oh-my-zsh)
# --------------------------
detect_nvm() {
# Check for core nvm files (regardless of installation method)
local nvm_sh_path="$HOME/.nvm/nvm.sh"
local is_oh_my_zsh=false
local has_nvm_plugin=false
# Detect oh-my-zsh environment
if [ -d "$HOME/.oh-my-zsh" ] && [ -f "$HOME/.zshrc" ]; then
is_oh_my_zsh=true
# Check if nvm plugin is loaded in zsh config
if grep -q 'plugins=.*nvm' "$HOME/.zshrc"; then
has_nvm_plugin=true
fi
fi
# Case 1: nvm is installed (core files exist)
if [ -f "$nvm_sh_path" ]; then
info "nvm installation detected"
# Use oh-my-zsh plugin loading if available
if [ "$is_oh_my_zsh" = true ] && [ "$has_nvm_plugin" = true ]; then
info "Detected oh-my-zsh with nvm plugin - using plugin loading"
# Load once for current session
[ -s "$nvm_sh_path" ] && \. "$nvm_sh_path"
return 0
else
info "Loading nvm environment variables manually"
export NVM_DIR="$HOME/.nvm"
[ -s "$nvm_sh_path" ] && \. "$nvm_sh_path"
return 0
fi
else
# Case 2: nvm not installed
error "nvm not found ($nvm_sh_path missing). Please install nvm first (via oh-my-zsh plugin or official method)"
fi
}
# --------------------------
# Main execution
# --------------------------
info "===== Starting Node.js 20.x LTS update process ====="
# 1. Detect and load nvm
detect_nvm
# 2. Verify nvm command is available
if ! command -v nvm &> /dev/null; then
error "nvm failed to load. Please restart your terminal and try again"
fi
# 3. Show current Node version
current_version=$(nvm current)
info "Currently active Node version: $current_version"
# 4. Install latest 20.x LTS with package migration
info "Installing latest Node.js 20.x LTS version..."
if [[ $current_version == v20.* ]]; then
# Migrate global packages from current 20.x version
nvm install 20 --reinstall-packages-from=20
else
# Try to migrate from existing 20.x if available
if nvm ls | grep -q "v20\."; then
local existing_20_version=$(nvm ls | grep "v20\." | tail -1 | awk '{print $2}')
nvm install 20 --reinstall-packages-from="$existing_20_version"
else
# Fresh install of 20.x
nvm install 20
fi
fi
# 5. Switch to newly installed version
info "Switching to latest Node.js 20.x version..."
nvm use 20
# 6. Update npm to latest compatible version
info "Updating npm to latest compatible version..."
npm install -g npm
# 7. Update all global CLI packages
info "Checking for global CLI packages to update..."
global_packages=$(npm list -g --depth=0 | awk -F '├──|└──' '/@|npm/ && !/npm@/ {print $2}' | xargs)
if [ -n "$global_packages" ]; then
info "Found global packages: $global_packages"
info "Updating all global packages..."
npm update -g
else
info "No global packages found to update"
fi
# 8. Special handling for claude-code (force reinstall for compatibility)
info "Verifying @anthropic-ai/claude-code compatibility..."
if npm list -g @anthropic-ai/claude-code &> /dev/null; then
info "Reinstalling @anthropic-ai/claude-code for compatibility..."
npm uninstall -g @anthropic-ai/claude-code
npm install -g @anthropic-ai/claude-code
else
info "@anthropic-ai/claude-code not installed - skipping"
fi
# 9. Final version information
success "===== Update completed successfully ====="
echo "Node.js version: $(node -v)"
echo "npm version: $(npm -v)"
info "Note: If you encounter issues with npx, try clearing cache with: npx clear-npx-cache"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment