From f20ce47dbb142adfc736d592418866527be5b1cb Mon Sep 17 00:00:00 2001 From: dr-js Date: Mon, 25 Jan 2021 11:43:20 +0800 Subject: [PATCH] doc,child_process: `pid` can be `undefined` when `ENOENT` From the code `nodejs@8` and up should behave the same: github.com/nodejs/node/blame/v8.17.0/lib/internal/child_process.js#L290 And a short test snippet: ```js const { spawn } = require('child_process') const subProcess = spawn('non-exist-command') subProcess.on('error', (error) => console.warn('mute Unhandled "error" event:', error)) console.log('- pid:', subProcess.pid) process.nextTick(() => console.log('- after error emit')) console.log('== end of test ==') ``` Note: the sync spawn result `pid` currently do not follow this pattern. Co-authored-by: Rich Trott PR-URL: https://github.com/nodejs/node/pull/37014 Reviewed-By: Antoine du Hamel --- doc/api/child_process.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 8d3186cbc4090d..ee8f0b4f0bc939 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1253,9 +1253,11 @@ does not indicate that the child process has been terminated. added: v0.1.90 --> -* {integer} +* {integer|undefined} -Returns the process identifier (PID) of the child process. +Returns the process identifier (PID) of the child process. If the child process +fails to spawn due to errors, then the value is `undefined` and `error` is +emitted. ```js const { spawn } = require('child_process');