SDK: misleading 'executable not found' error when PATH is missing from child env

Resolved 💬 3 comments Opened Apr 14, 2026 by cyrusagent Closed Apr 18, 2026

Bug

When env is passed to query() without PATH, the SDK spawns node cli.js but the OS can't find node (ENOENT). The SDK misinterprets this as cli.js not being found and throws:

ReferenceError: Claude Code executable not found at /path/to/cli.js. Is options.pathToClaudeCodeExecutable set?

This is misleading — cli.js exists and pathToClaudeCodeExecutable is set correctly. The actual problem is node not being on PATH.

Reproduction

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

for await (const msg of query({
  prompt: "hello",
  options: {
    env: {
      // No PATH — node binary can't be found
      SOME_VAR: "value",
    },
  },
})) {
  console.log(msg);
}

Root cause

In sdk.mjs, the spawn error handler checks NK(error) (likely ENOENT) and blames cli.js:

if (NK(p$)) {
  let j4 = o0
    ? `Claude Code native binary not found at ${G}. Please ensure Claude Code is installed...`
    : `Claude Code executable not found at ${G}. Is options.pathToClaudeCodeExecutable set?`;
  this.exitError = ReferenceError(j4);
}

But ENOENT can come from the command (node/bun) not being found, not just the script argument. The error handler doesn't distinguish between the two.

Suggested fix

Either:

  1. Check if the ENOENT is for the command vs the script argument
  2. Include both paths in the error message: "Failed to spawn '${executable} ${pathToClaudeCodeExecutable}': ${error.message}"
  3. Or at minimum, mention that PATH might be the issue when env is explicitly provided

Environment

  • @anthropic-ai/claude-agent-sdk@0.2.90
  • macOS, Node.js v22.17.1
  • pnpm monorepo (symlinked node_modules)

🤖 Generated with Claude Code

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗