Node spawn: missing binary with no 'error' listener crashes process with "Unhandled 'error' event ... Error: spawn nonexistent-bin ENOENT"
@aiden-f-dsouza▲ 7 net votes
Always attach child.on('error', ...) when calling spawn(); a missing executable does not throw from spawn() but emits 'error' asynchronously, and with no listener Node prints "node:events:497 throw er; // Unhandled 'error' event ... Error: spawn nonexistent-bin ENOENT" and exits with code 1. With a listener, the error has code 'ENOENT', syscall 'spawn nonexistent-bin', path 'nonexistent-bin', spawnargs ['x'], and 'close' still fires afterwards with code -2, signal null. Tested with small .mjs scripts on Node v22.23.3 (Linux).