Skip to content

Commit 1b6d7c0

Browse files
vsemozhetbyttargos
authored andcommitted
doc: fix nits in child_process.md
* Use `console.error()` for error or stderr output. * Unify comment style. * Unify link format. * Correct link URL. * Fix some typos. PR-URL: #29024 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 647f3a8 commit 1b6d7c0

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

doc/api/child_process.md

+26-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ls.stdout.on('data', (data) => {
1717
});
1818

1919
ls.stderr.on('data', (data) => {
20-
console.log(`stderr: ${data}`);
20+
console.error(`stderr: ${data}`);
2121
});
2222

2323
ls.on('close', (code) => {
@@ -93,7 +93,7 @@ When running on Windows, `.bat` and `.cmd` files can be invoked using
9393
spaces it needs to be quoted.
9494

9595
```js
96-
// On Windows Only ...
96+
// On Windows Only...
9797
const { spawn } = require('child_process');
9898
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
9999

@@ -102,7 +102,7 @@ bat.stdout.on('data', (data) => {
102102
});
103103

104104
bat.stderr.on('data', (data) => {
105-
console.log(data.toString());
105+
console.error(data.toString());
106106
});
107107

108108
bat.on('exit', (code) => {
@@ -171,10 +171,10 @@ need to be dealt with accordingly:
171171
```js
172172
exec('"/path/to/test file/test.sh" arg1 arg2');
173173
// Double quotes are used so that the space in the path is not interpreted as
174-
// multiple arguments
174+
// a delimiter of multiple arguments.
175175

176176
exec('echo "The \\$HOME variable is $HOME"');
177-
// The $HOME variable is escaped in the first instance, but not in the second
177+
// The $HOME variable is escaped in the first instance, but not in the second.
178178
```
179179

180180
**Never pass unsanitized user input to this function. Any input containing shell
@@ -202,7 +202,7 @@ exec('cat *.js missing_file | wc -l', (error, stdout, stderr) => {
202202
return;
203203
}
204204
console.log(`stdout: ${stdout}`);
205-
console.log(`stderr: ${stderr}`);
205+
console.error(`stderr: ${stderr}`);
206206
});
207207
```
208208

@@ -218,7 +218,7 @@ a `Promise` for an `Object` with `stdout` and `stderr` properties. The returned
218218
`ChildProcess` instance is attached to the `Promise` as a `child` property. In
219219
case of an error (including any error resulting in an exit code other than 0), a
220220
rejected promise is returned, with the same `error` object given in the
221-
callback, but with an additional two properties `stdout` and `stderr`.
221+
callback, but with two additional properties `stdout` and `stderr`.
222222

223223
```js
224224
const util = require('util');
@@ -227,7 +227,7 @@ const exec = util.promisify(require('child_process').exec);
227227
async function lsExample() {
228228
const { stdout, stderr } = await exec('ls');
229229
console.log('stdout:', stdout);
230-
console.log('stderr:', stderr);
230+
console.error('stderr:', stderr);
231231
}
232232
lsExample();
233233
```
@@ -300,7 +300,7 @@ a `Promise` for an `Object` with `stdout` and `stderr` properties. The returned
300300
`ChildProcess` instance is attached to the `Promise` as a `child` property. In
301301
case of an error (including any error resulting in an exit code other than 0), a
302302
rejected promise is returned, with the same `error` object given in the
303-
callback, but with an additional two properties `stdout` and `stderr`.
303+
callback, but with two additional properties `stdout` and `stderr`.
304304

305305
```js
306306
const util = require('util');
@@ -458,7 +458,7 @@ ls.stdout.on('data', (data) => {
458458
});
459459

460460
ls.stderr.on('data', (data) => {
461-
console.log(`stderr: ${data}`);
461+
console.error(`stderr: ${data}`);
462462
});
463463

464464
ls.on('close', (code) => {
@@ -478,7 +478,7 @@ ps.stdout.on('data', (data) => {
478478
});
479479

480480
ps.stderr.on('data', (data) => {
481-
console.log(`ps stderr: ${data}`);
481+
console.error(`ps stderr: ${data}`);
482482
});
483483

484484
ps.on('close', (code) => {
@@ -493,7 +493,7 @@ grep.stdout.on('data', (data) => {
493493
});
494494

495495
grep.stderr.on('data', (data) => {
496-
console.log(`grep stderr: ${data}`);
496+
console.error(`grep stderr: ${data}`);
497497
});
498498

499499
grep.on('close', (code) => {
@@ -510,7 +510,7 @@ const { spawn } = require('child_process');
510510
const subprocess = spawn('bad_command');
511511

512512
subprocess.on('error', (err) => {
513-
console.log('Failed to start subprocess.');
513+
console.error('Failed to start subprocess.');
514514
});
515515
```
516516

@@ -609,8 +609,8 @@ pipes between the parent and child. The value is one of the following:
609609

610610
1. `'pipe'` - Create a pipe between the child process and the parent process.
611611
The parent end of the pipe is exposed to the parent as a property on the
612-
`child_process` object as [`subprocess.stdio[fd]`][`stdio`]. Pipes created
613-
for fds 0 - 2 are also available as [`subprocess.stdin`][],
612+
`child_process` object as [`subprocess.stdio[fd]`][`subprocess.stdio`]. Pipes
613+
created for fds 0 - 2 are also available as [`subprocess.stdin`][],
614614
[`subprocess.stdout`][] and [`subprocess.stderr`][], respectively.
615615
2. `'ipc'` - Create an IPC channel for passing messages/file descriptors
616616
between parent and child. A [`ChildProcess`][] may have at most one IPC
@@ -648,10 +648,10 @@ pipes between the parent and child. The value is one of the following:
648648
```js
649649
const { spawn } = require('child_process');
650650

651-
// Child will use parent's stdios
651+
// Child will use parent's stdios.
652652
spawn('prg', [], { stdio: 'inherit' });
653653

654-
// Spawn child sharing only stderr
654+
// Spawn child sharing only stderr.
655655
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
656656

657657
// Open an extra fd=4, to interact with programs presenting a
@@ -1033,7 +1033,7 @@ process of being received. This will most often be triggered immediately after
10331033
calling `subprocess.disconnect()`.
10341034

10351035
When the child process is a Node.js instance (e.g. spawned using
1036-
[`child_process.fork()`]), the `process.disconnect()` method can be invoked
1036+
[`child_process.fork()`][]), the `process.disconnect()` method can be invoked
10371037
within the child process to close the IPC channel as well.
10381038

10391039
### subprocess.kill([signal])
@@ -1056,7 +1056,7 @@ grep.on('close', (code, signal) => {
10561056
`child process terminated due to receipt of signal ${signal}`);
10571057
});
10581058

1059-
// Send SIGHUP to process
1059+
// Send SIGHUP to process.
10601060
grep.kill('SIGHUP');
10611061
```
10621062

@@ -1092,7 +1092,7 @@ const subprocess = spawn(
10921092
);
10931093

10941094
setTimeout(() => {
1095-
subprocess.kill(); // Does not terminate the node process in the shell
1095+
subprocess.kill(); // Does not terminate the Node.js process in the shell.
10961096
}, 2000);
10971097
```
10981098

@@ -1291,12 +1291,12 @@ const special = fork('subprocess.js', ['special']);
12911291
const server = require('net').createServer({ pauseOnConnect: true });
12921292
server.on('connection', (socket) => {
12931293

1294-
// If this is special priority
1294+
// If this is special priority...
12951295
if (socket.remoteAddress === '74.125.127.100') {
12961296
special.send('socket', socket);
12971297
return;
12981298
}
1299-
// This is normal priority
1299+
// This is normal priority.
13001300
normal.send('socket', socket);
13011301
});
13021302
server.listen(1337);
@@ -1384,9 +1384,9 @@ const child_process = require('child_process');
13841384

13851385
const subprocess = child_process.spawn('ls', {
13861386
stdio: [
1387-
0, // Use parent's stdin for child
1388-
'pipe', // Pipe child's stdout to parent
1389-
fs.openSync('err.out', 'w') // Direct child's stderr to a file
1387+
0, // Use parent's stdin for child.
1388+
'pipe', // Pipe child's stdout to parent.
1389+
fs.openSync('err.out', 'w') // Direct child's stderr to a file.
13901390
]
13911391
});
13921392

@@ -1499,6 +1499,7 @@ unavailable.
14991499
[`subprocess.send()`]: #child_process_subprocess_send_message_sendhandle_options_callback
15001500
[`subprocess.stderr`]: #child_process_subprocess_stderr
15011501
[`subprocess.stdin`]: #child_process_subprocess_stdin
1502+
[`subprocess.stdio`]: #child_process_subprocess_stdio
15021503
[`subprocess.stdout`]: #child_process_subprocess_stdout
15031504
[`util.promisify()`]: util.html#util_util_promisify_original
15041505
[Default Windows Shell]: #child_process_default_windows_shell

0 commit comments

Comments
 (0)