Skip to content

Claude Code CLI crashes with ReferenceError: Bun is not defined when spawned under Node.js #266

@RTodorov

Description

@RTodorov

SDK version: @anthropic-ai/claude-agent-sdk@0.2.29
CLI version: @anthropic-ai/claude-code (latest via npm, running on node:22-alpine Docker image)
Runtime: Node.js 22 (not Bun)

The Claude Code CLI bundle (cli.js) contains code paths that reference the Bun global directly without a typeof guard, causing an intermittent ReferenceError when the CLI is spawned under Node.js.

The crash is intermittent because it only triggers when specific code paths are reached during a conversation (e.g., Bun.which(...) for executable resolution). It manifests as Claude Code process exited with code 1 with no stderr output, making it extremely difficult to diagnose.

Reproduction

  import { query } from "@anthropic-ai/claude-agent-sdk";

  // Host process runs on Node.js, so SDK defaults executable to "node"
  // CLI is spawned as: node /path/to/cli.js --output-format stream-json ...
  const response = query({
    prompt: "Help me with this codebase",
    options: {
      cwd: "/some/workspace",
      permissionMode: "bypassPermissions",
    }
  });

  // Works initially, but crashes intermittently mid-conversation
  // when a code path in cli.js accesses `Bun` without a typeof check
  for await (const message of response) {
    console.log(message);
  }

Error

ReferenceError: Bun is not defined

No stack trace is available in stderr. The error was only captured after enabling --debug-file via extraArgs.

Root cause

The CLI bundle contains both guarded and unguarded Bun references:

  // Safe — typeof never throws a ReferenceError
  ECq = typeof Bun < "u" ? ...
  mZ = typeof Bun < "u" ? vCq : kCq

  // Unsafe — crashes under Node.js when this function is called
  async function vCq(A) { return Bun.which(A) }

While the conditional assignment of mZ is guarded, the function vCq itself is defined unconditionally. If any code path ends up calling vCq despite mZ being set to the Node.js variant kCq, or if there are other unguarded Bun references, the process crashes.

Impact

  • The crash produces no stderr output — the only signal is exit code 1
  • The SDK surfaces the generic error Claude Code process exited with code 1 with no diagnostic detail
  • The --debug-file flag (the only way to capture the actual error) also appears to use Bun-specific file I/O, requiring the workaround below to function

Workaround

Install Bun and explicitly set executable: "bun" in SDK options:

  const response = query({
    prompt: "...",
    options: {
      executable: "bun",  // Force CLI to run under Bun instead of Node.js
      // ...
    }
  });

Expected behavior

The Claude Code CLI should work correctly when spawned under Node.js, which is the SDK's default behavior when the host process is not Bun.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions