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 ""
claude-guard --help
npx claude-guard --help
# 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
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
The error happens because:
- ES modules require specific Node.js flags
- Sudo environment may have different Node.js configuration
- The shebang line might not include necessary flags
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