diff --git a/lib/child_process.js b/lib/child_process.js index 09369316de52f1..d8c91506fbdaa7 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -875,11 +875,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');