镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,51 +118,45 @@ operating systems (Unix, Linux, macOS) [`child_process.execFile()`][] can be
more efficient because it does not spawn a shell by default. On Windows,
however, `.bat` and `.cmd` files are not executable on their own without a
terminal, and therefore cannot be launched using [`child_process.execFile()`][].
When running on Windows, `.bat` and `.cmd` files can be invoked using
[`child_process.spawn()`][] with the `shell` option set, with
[`child_process.exec()`][], or by spawning `cmd.exe` and passing the `.bat` or
`.cmd` file as an argument (which is what the `shell` option and
[`child_process.exec()`][] do). In any case, if the script filename contains
spaces it needs to be quoted.
When running on Windows, `.bat` and `.cmd` files can be invoked by:

* using [`child_process.spawn()`][] with the `shell` option set, or
* using [`child_process.exec()`][], or
* spawning `cmd.exe` and passing the `.bat` or `.cmd` file as an argument
(which is what the `shell` option and [`child_process.exec()`][] do).

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

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

exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
// 1. child_process.spawn() with the shell option set
const myBat = spawn('my.bat', { shell: true });

// Script with spaces in the filename:
const bat = spawn('"my script.cmd" a b', { shell: true });
// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
// ...
});
// 2. child_process.exec()
exec('my.bat', (err, stdout, stderr) => { /* ... */ });

// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
const bat = spawn('cmd.exe', ['/c', 'my.bat']);

// If the script filename contains spaces, it needs to be quoted
exec('"my script.cmd" a b', (err, stdout, stderr) => { /* ... */ });
```

```mjs
// OR...
import { exec, spawn } from 'node:child_process';

exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
// 1. child_process.spawn() with the shell option set
const myBat = spawn('my.bat', { shell: true });

// Script with spaces in the filename:
const bat = spawn('"my script.cmd" a b', { shell: true });
// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
// ...
});
// 2. child_process.exec()
exec('my.bat', (err, stdout, stderr) => { /* ... */ });

// 3. spawning cmd.exe and passing the .bat or .cmd file as an argument
const bat = spawn('cmd.exe', ['/c', 'my.bat']);

// If the script filename contains spaces, it needs to be quoted
exec('"my script.cmd" a b', (err, stdout, stderr) => { /* ... */ });
```

### `child_process.exec(command[, options][, callback])`
Expand Down
Loading