diff --git a/lib/child_process.js b/lib/child_process.js index 8c10ce36cc7d74..e9c52eef7e21ba 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -870,11 +870,14 @@ function spawnWithSignal(file, args, options) { const opts = options && typeof options === 'object' && ('signal' in options) ? { ...options, signal: undefined } : options; - const child = spawn(file, args, opts); - if (options && options.signal) { + if (options?.signal) { // Validate signal, if present validateAbortSignal(options.signal, 'options.signal'); + } + const child = spawn(file, args, opts); + + if (options && options.signal) { function kill() { if (child._handle) { child._handle.kill(options.killSignal || 'SIGTERM');