Skip to content
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

child_process: add example for execFileSync method and ref to stdio #39412

Merged
merged 1 commit into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ changes:
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. If `stdio[0]` is set to `'pipe'`, Supplying
this value will override `stdio[0]`.
* `stdio` {string|Array} Child's stdio configuration. `stderr` by default will
* `stdio` {string|Array} Child's stdio configuration.
See [`child_process.spawn()`][]'s [`stdio`][]. `stderr` by default will
be output to the parent process' stderr unless `stdio` is specified.
**Default:** `'pipe'`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
Expand Down Expand Up @@ -962,6 +963,34 @@ If the process times out or has a non-zero exit code, this method will throw an
function. Any input containing shell metacharacters may be used to trigger
arbitrary command execution.**

```js
const { execFileSync } = require('node:child_process');

try {
const stdout = execFileSync('my-script.sh', ['my-arg'], {
// Capture stdout and stderr from child process. Overrides the
// default behavior of streaming child stderr to the parent stderr
stdio: 'pipe',

// Use utf8 encoding for stdio pipes
encoding: 'utf8',
});

console.log(stdout);
} catch (err) {
if (err.code) {
// Spawning child process failed
console.error(err.code);
} else {
// Child was spawned but exited with non-zero exit code
// Error contains any stdout and stderr from the child
const { stdout, stderr } = err;

console.error({ stdout, stderr });
}
}
```

### `child_process.execSync(command[, options])`

<!-- YAML
Expand Down Expand Up @@ -991,7 +1020,8 @@ changes:
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. If `stdio[0]` is set to `'pipe'`, Supplying
this value will override `stdio[0]`.
* `stdio` {string|Array} Child's stdio configuration. `stderr` by default will
* `stdio` {string|Array} Child's stdio configuration.
See [`child_process.spawn()`][]'s [`stdio`][]. `stderr` by default will
be output to the parent process' stderr unless `stdio` is specified.
**Default:** `'pipe'`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
Expand Down Expand Up @@ -1069,7 +1099,8 @@ changes:
this value will override `stdio[0]`.
* `argv0` {string} Explicitly set the value of `argv[0]` sent to the child
process. This will be set to `command` if not specified.
* `stdio` {string|Array} Child's stdio configuration. **Default:** `'pipe'`.
* `stdio` {string|Array} Child's stdio configuration.
See [`child_process.spawn()`][]'s [`stdio`][]. **Default:** `'pipe'`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `uid` {number} Sets the user identity of the process (see setuid(2)).
* `gid` {number} Sets the group identity of the process (see setgid(2)).
Expand Down