Skip to content

Instantly share code, notes, and snippets.

@sxalexander
Created December 31, 2025 19:15
Show Gist options
  • Select an option

  • Save sxalexander/e6d70f9966cc56ea1c55256d66a24559 to your computer and use it in GitHub Desktop.

Select an option

Save sxalexander/e6d70f9966cc56ea1c55256d66a24559 to your computer and use it in GitHub Desktop.
claude-force-reinstall
#!/bin/bash
# Force reinstall Claude Code when it's borked
# Run this when: claude command not found, permission errors, or other weirdness
set -e
echo "πŸ”§ Force reinstalling Claude Code..."
echo ""
# Find the npm global prefix (works across different node versions)
NPM_PREFIX=$(npm config get prefix)
# Safety check: ensure npm prefix looks reasonable
if [[ -z "$NPM_PREFIX" ]] || [[ "$NPM_PREFIX" == "/" ]]; then
echo "❌ Error: npm prefix looks invalid: '$NPM_PREFIX'"
exit 1
fi
echo "πŸ“ Found npm prefix: $NPM_PREFIX"
# Try npm uninstall first (the safe way)
echo "πŸ—‘οΈ Removing old Claude Code installation..."
if npm uninstall -g @anthropic-ai/claude-code 2>/dev/null; then
echo " βœ“ Uninstalled via npm"
else
echo " ℹ️ npm uninstall failed (package may be corrupted)"
# Only remove if the path contains expected structure
CLAUDE_DIR="$NPM_PREFIX/lib/node_modules/@anthropic-ai"
if [[ -d "$CLAUDE_DIR" ]] && [[ "$CLAUDE_DIR" == *"node_modules/@anthropic-ai"* ]]; then
echo " βœ“ Cleaning up manually (validated path)"
npm exec -- npx rimraf "$CLAUDE_DIR" 2>/dev/null || {
# Fallback to manual cleanup only if rimraf not available
[ -d "$CLAUDE_DIR/claude-code" ] && mv "$CLAUDE_DIR/claude-code" "$CLAUDE_DIR/.claude-code-backup-$$"
[ -d "$CLAUDE_DIR/.claude-code-"* ] && find "$CLAUDE_DIR" -maxdepth 1 -name '.claude-code-*' -type d -exec rm -rf {} +
[ -d "$CLAUDE_DIR/.claude-code-backup-$$" ] && rm -rf "$CLAUDE_DIR/.claude-code-backup-$$"
}
fi
fi
# Clean npm cache
echo "🧹 Cleaning npm cache..."
npm cache clean --force 2>/dev/null || true
# Fresh install
echo "πŸ“¦ Installing fresh Claude Code..."
npm install -g @anthropic-ai/claude-code
echo ""
echo "βœ… Verification:"
which claude
claude --version
echo ""
echo "πŸŽ‰ Claude Code reinstalled successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment