SDK spawn reports misleading ENOENT when cwd does not exist
Summary
When query() is called with a cwd that does not exist on the filesystem, Node.js child_process.spawn() throws:
Error: Failed to spawn Claude Code process: spawn /opt/homebrew/bin/claude ENOENT
This error strongly implies the executable is missing, but the actual cause is the working directory does not exist. This is extremely misleading and leads users down the wrong debugging path (reinstalling Claude Code, changing executable paths, rolling back SDK versions, etc.).
Steps to Reproduce
import { query } from "@anthropic-ai/claude-agent-sdk";
const stream = query({
prompt: "hello",
options: {
cwd: "/nonexistent/directory", // <-- this is the real problem
pathToClaudeCodeExecutable: "/opt/homebrew/bin/claude", // <-- error blames this
}
});
Expected Behavior
The SDK should validate that cwd exists before calling spawn(), and throw a clear error like:
Error: Working directory does not exist: /nonexistent/directory
Actual Behavior
Error: Failed to spawn Claude Code process: spawn /opt/homebrew/bin/claude ENOENT
Environment
- SDK version: 0.2.62
- Node.js: v25.6.1
- OS: macOS (Darwin 25.3.0, arm64)
Root Cause
This is a known Node.js behavior — spawn() reports ENOENT with the executable path even when the real issue is an invalid cwd. The SDK already validates the executable with existsSync() before spawning, but does not validate cwd.
Suggested Fix
Add a cwd existence check before the spawn call, similar to the existing executable check:
if (cwd && !fs.existsSync(cwd)) {
throw new Error(`Working directory does not exist: ${cwd}`);
}This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗