Swarm commands fail with "require is not defined" error
- Dominant language
- TypeScript
- Stars
- 72.6k
- Forks
- 8.6k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 85
Description
When attempting to use swarm commands via CLI, the execution fails with an ES module compatibility error. The command tries to use a compiled module that doesn't exist, then falls back to a built-in executor that encounters a module loading
issue.
Steps to Reproduce
1. Install claude-flow: npm install -g claude-flow@alpha
2. Run any swarm command:
- npx claude-flow swarm "task"
- ./claude-flow swarm "task"
- npx claude-flow@alpha swarm init --topology mesh
3. Observe error: "Failed to spawn Claude Code: require is not defined"
Expected Behavior
Swarm commands should execute successfully and orchestrate multi-agent tasks.
Actual Behavior
Commands fail with:
Falling back to built-in executor...
📦 Compiled swarm module not found, checking for Claude CLI...
❌ Failed to spawn Claude Code: require is not defined
Environment
- Node.js: v24.5.0
- npm: 11.5.1
- claude-flow: 2.0.0-alpha.90
- OS: Linux (WSL2)
- Missing: ~/.claude-flow/compiled/ directory
Root Cause Analysis
1. The swarm command looks for compiled modules at ~/.claude-flow/compiled/ which don't exist
2. Fallback executor has ES module/CommonJS compatibility issues
3. The local wrapper script uses dynamic imports but encounters module context problems
Workaround
Use MCP tools directly instead of CLI:
// Works via MCP
mcp__claude-flow__swarm_init { topology: "mesh" }
mcp__claude-flow__agent_spawn { type: "coder" }
mcp__claude-flow__task_orchestrate { task: "..." }
Suggested Fixes
1. Add compilation step: Auto-compile required modules during installation
2. Fix module compatibility: Update fallback executor to handle both ES modules and CommonJS
3. Improve error messages: Provide clearer guidance when compiled modules are missing
4. Update documentation: Document MCP tools as primary method while CLI is being fixed
5. Add fallback to MCP: When CLI fails, automatically attempt MCP tool execution
Additional Notes
- Other claude-flow commands work fine (status, agent spawn, etc.)
- MCP tools provide full swarm functionality as a workaround
- Issue appears specific to the swarm command's execution strategy
Priority
High - Core functionality (swarm orchestration) is broken via CLI interface
Contributor guide
Assessment
This issue has not been assessed yet.