Fix: Handle npm EACCES permission errors gracefully

Root Cause: npm install -g @z_ai/coding-helper was failing with EACCES
(permission denied) when trying to create /usr/local/lib/node_modules/@z_ai,
but the error was being suppressed by 2>/dev/null in the original version.

The script would report "Installation completed" even though it failed.

Solution: Enhanced error handling with three paths:

1. Success Path (exit code = 0):
   - Verify installation with 'npm list -g'
   - Show "installed and verified!" message
   - Offer to launch wizard

2. Permission Error Path (EACCES detected):
   - Clear error message explaining the permission issue
   - Show 3 options:
     a) Use sudo (with interactive retry)
     b) Fix npm permissions permanently (chown command)
     c) Run without installation using npx
   - Interactive prompt to try sudo immediately

3. Other Error Path:
   - Show full error output
   - Suggest manual installation with sudo
   - Remind that npx works without installation

Changes:
- install_coding_helper(): Added EACCES detection, sudo retry option,
  and clear error messages for all failure scenarios
- Error output is now captured and displayed for debugging
- Added verification step after sudo installation

The script now properly handles the common case where users don't have
write permissions to /usr/local/lib/node_modules and offers multiple
solutions including an interactive sudo retry.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
uroma
2026-01-22 16:34:20 +00:00
Unverified
parent 0312e600f2
commit d4bfb4bf80

View File

@@ -723,16 +723,67 @@ install_coding_helper() {
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi
else
log_error "Installation failed!"
echo ""
echo -e "${YELLOW}Error output:${NC}"
echo "$install_output"
echo ""
echo -e "${CYAN}You can try installing manually:${NC}"
echo -e " ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${CYAN}Or run without installation:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
# Check if it's a permission error (EACCES)
if echo "$install_output" | grep -q "EACCES\|permission denied"; then
log_error "Installation failed due to permissions!"
echo ""
echo -e "${YELLOW}The installation requires write permissions to:${NC}"
echo -e " ${CYAN}/usr/local/lib/node_modules/@z_ai${NC}"
echo ""
echo -e "${BOLD}You have several options:${NC}"
echo ""
echo -e "${GREEN}Option 1: Use sudo (recommended)${NC}"
echo -e " Run: ${YELLOW}sudo npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${GREEN}Option 2: Fix npm permissions permanently${NC}"
echo -e " Run: ${YELLOW}sudo chown -R \$(whoami) /usr/local/lib/node_modules${NC}"
echo -e " ${YELLOW}sudo chown -R \$(whoami) /usr/local/bin${NC}"
echo -e " Then: ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${GREEN}Option 3: Run without installation (works immediately)${NC}"
echo -e " Just use: ${YELLOW}npx @z_ai/coding-helper${NC}"
echo ""
echo -e "${CYAN}Want to try with sudo now? [y/N]${NC}"
read -p " " -n 1 -r use_sudo < /dev/tty
echo ""
if [[ $use_sudo =~ ^[Yy]$ ]]; then
echo ""
log_info "Installing with sudo..."
if sudo npm install -g @z_ai/coding-helper 2>&1; then
if npm list -g @z_ai/coding-helper &> /dev/null 2>&1; then
log_success "coding-helper installed and verified with sudo!"
echo ""
read -p "Launch coding-helper wizard now? [y/N] " -n 1 -r launch_helper < /dev/tty
echo ""
if [[ $launch_helper =~ ^[Yy]$ ]]; then
launch_coding_helper
fi
fi
else
log_warn "Sudo installation also failed"
echo ""
echo -e "${CYAN}You can still run it without installation:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi
else
echo ""
echo -e "${CYAN}No problem! You can run it anytime with:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi
else
# Non-permission error
log_error "Installation failed!"
echo ""
echo -e "${YELLOW}Error output:${NC}"
echo "$install_output"
echo ""
echo -e "${CYAN}You can try installing manually:${NC}"
echo -e " ${YELLOW}npm install -g @z_ai/coding-helper${NC}"
echo -e " ${YELLOW}sudo npm install -g @z_ai/coding-helper${NC}"
echo ""
echo -e "${CYAN}Or run without installation:${NC}"
echo -e " ${YELLOW}npx @z_ai/coding-helper${NC}"
fi
fi
}