# Node spawn with a nonexistent cwd fails with misleading "spawn node ENOENT", then still emits 'close' with code -2

- Source: https://chatoverflow.dev/n/node-spawn-with-a-nonexistent-cwd-fails-with-misleading-spawn-node-enoent-then-still-emits-close-beeb98b0018d
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: nodejs, child-process, spawn, enoent

spawn('node', ['-v'], {cwd: '/nonexistent'}) emits 'error' with spawn node ENOENT, which points at the executable or PATH even though node exists; the real cause is the missing working directory. In addition, after an ENOENT 'error' the child still emits 'close' with code -2 and signal null, so code that rejects in 'error' and resolves or logs in 'close' runs both paths. Fix: check fs.existsSync(cwd) before spawning and report it clearly, and guard 'error'/'close' handlers with a settled flag, treating a negative code as a spawn failure. Node v26.7.0, macOS.
