Skip to content

Instantly share code, notes, and snippets.

@dwillitzer
Created June 2, 2025 16:32
Show Gist options
  • Save dwillitzer/ffebf59ece0a6483b0c72e24a04bf260 to your computer and use it in GitHub Desktop.
Save dwillitzer/ffebf59ece0a6483b0c72e24a04bf260 to your computer and use it in GitHub Desktop.
Claude Guard Sudo Execution Hotfix for dknoodle

Hotfix for Sudo Execution Error

Quick Fix Script

Save this as hotfix-sudo-issue.sh and run with sudo bash hotfix-sudo-issue.sh:

#!/bin/bash

# Claude Guard Sudo Hotfix Script
# Fixes the "Unexpected reserved word" error when running with sudo

echo "Claude Guard Sudo Hotfix"
echo "======================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "Please run this script with sudo"
    exit 1
fi

# Find global npm path
GLOBAL_NPM=$(npm root -g)
CLAUDE_GUARD_PATH="$GLOBAL_NPM/claude-guard"

if [ ! -d "$CLAUDE_GUARD_PATH" ]; then
    echo "ERROR: claude-guard not found in global npm modules"
    echo "Path checked: $CLAUDE_GUARD_PATH"
    exit 1
fi

echo "Found claude-guard at: $CLAUDE_GUARD_PATH"
echo ""

# Check Node.js version
NODE_VERSION=$(node -v)
echo "Node.js version: $NODE_VERSION"

# Create a wrapper script that properly handles ES modules
echo "Creating compatibility wrapper..."

cat > /usr/local/bin/claude-guard-wrapper << 'EOF'
#!/usr/bin/env node

// Compatibility wrapper for claude-guard
const { spawn } = require('child_process');
const path = require('path');

// Find the global installation
const { execSync } = require('child_process');
const globalPath = execSync('npm root -g', { encoding: 'utf-8' }).trim();
const claudeGuardMain = path.join(globalPath, 'claude-guard', 'bin', 'claude-guard.js');

// Run with proper Node.js flags
const args = [
  '--no-warnings',
  '--experimental-modules',
  claudeGuardMain,
  ...process.argv.slice(2)
];

const child = spawn(process.execPath, args, {
  stdio: 'inherit',
  env: process.env
});

child.on('error', (err) => {
  console.error('Failed to start claude-guard:', err.message);
  process.exit(1);
});

child.on('exit', (code) => {
  process.exit(code || 0);
});
EOF

chmod +x /usr/local/bin/claude-guard-wrapper

# Update the symlink to use the wrapper
if [ -L /usr/local/bin/claude-guard ]; then
    rm /usr/local/bin/claude-guard
fi

ln -s /usr/local/bin/claude-guard-wrapper /usr/local/bin/claude-guard

echo ""
echo "✅ Hotfix applied successfully!"
echo ""
echo "You can now run claude-guard with sudo:"
echo "  sudo claude-guard --help"
echo ""

Alternative Solutions

1. Run without sudo

claude-guard --help

2. Use npx to run

npx claude-guard --help

3. Install with nvm (no sudo needed)

# Install nvm if not already installed
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# Use nvm to install Node.js
nvm install 20
nvm use 20

# Install claude-guard without sudo
npm install -g claude-guard

# Run without sudo
claude-guard --help

4. Manual Fix

If the above doesn't work, manually edit the shebang line:

# Find where claude-guard is installed
which claude-guard

# Edit the file (example path)
sudo nano /usr/local/lib/node_modules/claude-guard/bin/claude-guard.js

# Make sure the first line is:
#!/usr/bin/env -S node --no-warnings

# Or for older Node.js:
#!/usr/bin/env node

Root Cause

The error happens because:

  1. ES modules require specific Node.js flags
  2. Sudo environment may have different Node.js configuration
  3. The shebang line might not include necessary flags

Contact

If you still have issues, please let me know with:

  • Exact error message
  • Output of node --version
  • Output of sudo node --version
  • Output of which claude-guard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment