Created
December 31, 2025 19:15
-
-
Save sxalexander/e6d70f9966cc56ea1c55256d66a24559 to your computer and use it in GitHub Desktop.
claude-force-reinstall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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