Skip to content

Commit c41ca59

Browse files
committed
doc: remove spawn with shell example from bat/cmd section
Remove the suggestion to use child_process.spawn() with the shell option set for running .bat and .cmd files on Windows. Passing arguments through spawn with shell: true is deprecated (DEP0190) due to shell injection risks. Keep the exec() and direct cmd.exe spawn alternatives. Fixes: #58735
1 parent e42dbef commit c41ca59

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

doc/api/child_process.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,21 @@ however, `.bat` and `.cmd` files are not executable on their own without a
120120
terminal, and therefore cannot be launched using [`child_process.execFile()`][].
121121
When running on Windows, `.bat` and `.cmd` files can be invoked by:
122122

123-
* using [`child_process.spawn()`][] with the `shell` option set, or
124123
* using [`child_process.exec()`][], or
125124
* 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).
125+
(which is what [`child_process.exec()`][] does internally).
127126

128127
In any case, if the script filename contains spaces, it needs to be quoted.
129128

129+
Using [`child_process.spawn()`][] with the `shell` option is not recommended
130+
because passing arguments that way is deprecated ([DEP0190][]).
131+
130132
```cjs
131133
const { exec, spawn } = require('node:child_process');
132134

133-
// 1. child_process.spawn() with the shell option set
134-
const myBat = spawn('my.bat', { shell: true });
135-
136-
// 2. child_process.exec()
137135
exec('my.bat', (err, stdout, stderr) => { /* ... */ });
138136

139-
// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
137+
// Or, spawning cmd.exe directly:
140138
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
141139

142140
// If the script filename contains spaces, it needs to be quoted
@@ -146,13 +144,9 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => { /* ... */ });
146144
```mjs
147145
import { exec, spawn } from 'node:child_process';
148146

149-
// 1. child_process.spawn() with the shell option set
150-
const myBat = spawn('my.bat', { shell: true });
151-
152-
// 2. child_process.exec()
153147
exec('my.bat', (err, stdout, stderr) => { /* ... */ });
154148

155-
// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
149+
// Or, spawning cmd.exe directly:
156150
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
157151

158152
// If the script filename contains spaces, it needs to be quoted
@@ -2364,6 +2358,7 @@ Therefore, this feature requires opting in by setting the
23642358
or [`child_process.fork()`][].
23652359

23662360
[Advanced serialization]: #advanced-serialization
2361+
[DEP0190]: deprecations.md#dep0190-passing-args-to-nodechild_process-execfilespawn-with-shell-option-true
23672362
[Default Windows shell]: #default-windows-shell
23682363
[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
23692364
[Shell requirements]: #shell-requirements

0 commit comments

Comments
 (0)