Skip to content

doc: update "Spawning .bat and .cmd files on Windows" section #58739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 5 additions & 12 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,13 @@ 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 should only be invoked using
[`child_process.exec()`][] and if the script filename contains spaces it needs
to be quoted.

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

exec('my.bat', (err, stdout, stderr) => {
if (err) {
Expand All @@ -138,16 +135,14 @@ exec('my.bat', (err, stdout, stderr) => {
});

// Script with spaces in the filename:
const bat = spawn('"my script.cmd" a b', { shell: true });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't use args array, so this doesn't fall into the DEP0190 case

// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
// ...
});
```

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

exec('my.bat', (err, stdout, stderr) => {
if (err) {
Expand All @@ -158,8 +153,6 @@ exec('my.bat', (err, stdout, stderr) => {
});

// 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) => {
// ...
});
Expand Down