Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae5e353

Browse filesBrowse files
reillylmaduh95
authored andcommitted
doc: clean up Windows code snippet in child_process.md
PR-URL: #61422 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ea9beb6 commit ae5e353
Copy full SHA for ae5e353

1 file changed

+28-34Lines changed: 28 additions & 34 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/child_process.md‎

Copy file name to clipboardExpand all lines: doc/api/child_process.md
+28-34Lines changed: 28 additions & 34 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -118,51 +118,45 @@ operating systems (Unix, Linux, macOS) [`child_process.execFile()`][] can be
118118
more efficient because it does not spawn a shell by default. On Windows,
119119
however, `.bat` and `.cmd` files are not executable on their own without a
120120
terminal, and therefore cannot be launched using [`child_process.execFile()`][].
121-
When running on Windows, `.bat` and `.cmd` files can be invoked using
122-
[`child_process.spawn()`][] with the `shell` option set, with
123-
[`child_process.exec()`][], or by spawning `cmd.exe` and passing the `.bat` or
124-
`.cmd` file as an argument (which is what the `shell` option and
125-
[`child_process.exec()`][] do). In any case, if the script filename contains
126-
spaces it needs to be quoted.
121+
When running on Windows, `.bat` and `.cmd` files can be invoked by:
122+
123+
* using [`child_process.spawn()`][] with the `shell` option set, or
124+
* using [`child_process.exec()`][], or
125+
* spawning `cmd.exe` and passing the `.bat` or `.cmd` file as an argument
126+
(which is what the `shell` option and [`child_process.exec()`][] do).
127+
128+
In any case, if the script filename contains spaces, it needs to be quoted.
127129

128130
```cjs
129-
// OR...
130131
const { exec, spawn } = require('node:child_process');
131132

132-
exec('my.bat', (err, stdout, stderr) => {
133-
if (err) {
134-
console.error(err);
135-
return;
136-
}
137-
console.log(stdout);
138-
});
133+
// 1. child_process.spawn() with the shell option set
134+
const myBat = spawn('my.bat', { shell: true });
139135

140-
// Script with spaces in the filename:
141-
const bat = spawn('"my script.cmd" a b', { shell: true });
142-
// or:
143-
exec('"my script.cmd" a b', (err, stdout, stderr) => {
144-
// ...
145-
});
136+
// 2. child_process.exec()
137+
exec('my.bat', (err, stdout, stderr) => { /* ... */ });
138+
139+
// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
140+
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
141+
142+
// If the script filename contains spaces, it needs to be quoted
143+
exec('"my script.cmd" a b', (err, stdout, stderr) => { /* ... */ });
146144
```
147145

148146
```mjs
149-
// OR...
150147
import { exec, spawn } from 'node:child_process';
151148

152-
exec('my.bat', (err, stdout, stderr) => {
153-
if (err) {
154-
console.error(err);
155-
return;
156-
}
157-
console.log(stdout);
158-
});
149+
// 1. child_process.spawn() with the shell option set
150+
const myBat = spawn('my.bat', { shell: true });
159151

160-
// Script with spaces in the filename:
161-
const bat = spawn('"my script.cmd" a b', { shell: true });
162-
// or:
163-
exec('"my script.cmd" a b', (err, stdout, stderr) => {
164-
// ...
165-
});
152+
// 2. child_process.exec()
153+
exec('my.bat', (err, stdout, stderr) => { /* ... */ });
154+
155+
// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
156+
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
157+
158+
// If the script filename contains spaces, it needs to be quoted
159+
exec('"my script.cmd" a b', (err, stdout, stderr) => { /* ... */ });
166160
```
167161

168162
### `child_process.exec(command[, options][, callback])`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.