A better
child_process
- Promise interface.
- Strips the final newline from the output so you don't have to do
stdout.trim()
. - Supports shebang binaries cross-platform.
- Improved Windows support.
- Higher max buffer. 10 MB instead of 200 KB.
- Executes locally installed binaries by name.
- Cleans up spawned processes when the parent process dies.
- Adds an
.all
property with interleaved output fromstdout
andstderr
, similar to what the terminal sees. (Async only) - Can specify command and arguments as a single string without a shell
$ npm install execa
const execa = require('execa');
(async () => {
const {stdout} = await execa('echo', ['unicorns']);
console.log(stdout);
//=> 'unicorns'
})();
Additional examples:
const execa = require('execa');
(async () => {
// Pipe the child process stdout to the current stdout
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
// Catching an error
try {
await execa('wrong command');
} catch (error) {
console.log(error);
/*
{
message: 'spawn wrong command ENOENT',
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn wrong command',
path: 'wrong command',
killed: false,
stdout: '',
stderr: '',
failed: true,
signal: null,
cmd: 'wrong command',
timedOut: false
}
*/
}
// Cancelling a spawned process
const subprocess = execa('node');
setTimeout(() => {
subprocess.cancel();
}, 1000);
try {
await subprocess;
} catch (error) {
console.log(subprocess.killed); // true
console.log(error.isCanceled); // true
}
})();
// Catching an error with a sync method
try {
execa.sync('wrong command');
} catch (error) {
console.log(error);
/*
{
message: 'spawnSync wrong command ENOENT',
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawnSync wrong command',
path: 'wrong command',
}
*/
}
Execute a file.
Arguments can be specified in either:
arguments
:execa('echo', ['unicorns'])
.command
:execa('echo unicorns')
.
Arguments should not be escaped nor quoted. Exception: inside command
, spaces can be escaped with a backslash.
Think of this as a mix of child_process.execFile
and child_process.spawn
.
Unless the shell
option is used, no shell interpreter (Bash, cmd.exe
, etc.) is used, so shell features such as variables substitution (echo $PATH
) are not allowed.
Returns a child_process
instance which is enhanced to be a Promise
.
It exposes an additional .all
stream, with stdout
and stderr
interleaved.
The spawned process can be canceled with the .cancel()
method on the promi
8000
se, which causes the promise to reject an error with a .isCanceled = true
property, provided the process gets canceled. The process will not be canceled if it has already exited.
The promise result is an Object
with stdout
, stderr
and all
properties.
Same as execa()
, but returns only stdout
.
Same as execa()
, but returns only stderr
.
Execute a file synchronously.
Returns the same result object as child_process.spawnSync
.
It does not have the .all
property that execa()
has because the underlying synchronous implementation only returns stdout
and stderr
at the end of the execution, so they cannot be interleaved.
This method throws an Error
if the command fails.
Type: object
Type: string
Default: process.cwd()
Current working directory of the child process.
Type: object
Default: process.env
Environment key-value pairs. Extends automatically from process.env
. Set extendEnv
to false
if you don't want this.
Type: boolean
Default: true
Set to false
if you don't want to extend the environment variables when providing the env
property.
Type: string
Explicitly set the value of argv[0]
sent to the child process. This will be set to command
or file
if not specified.
Type: string | string[]
Default: pipe
Child's stdio configuration.
Type: boolean
Prepare child to run independently of its parent process. Specific behavior depends on the platform.
Type: number
Sets the user identity of the process.
Type: number
Sets the group identity of the process.
Type: boolean | string
Default: false
If true
, runs command
inside of a shell. Uses /bin/sh
on UNIX and cmd.exe
on Windows. A different shell can be specified as a string. The shell should understand the -c
switch on UNIX or /d /s /c
on Windows.
We recommend against using this option since it is:
- not cross-platform, encouraging shell-specific syntax.
- slower, because of the additional shell interpretation.
- unsafe, potentially allowing command injection.
Type: boolean
Default: true
Strip the final newline character from the output.
Type: boolean
Default: true
Prefer locally installed binaries when looking for a binary to execute.
If you $ npm install foo
, you can then execa('foo')
.
Type: string
Default: process.cwd()
Preferred path to find locally installed binaries in (use with preferLocal
).
Type: string | Buffer | stream.Readable
Write some input to the stdin
of your binary.
Streams are not allowed when using the synchronous methods.
Type: boolean
Default: true
Setting this to false
resolves the promise with the error instead of rejecting it.
Type: boolean
Default: true
Kill the spawned process when the parent process exits unless either:
- the spawned process is detached
- the parent process is terminated abruptly, for example, with SIGKILL
as opposed to SIGTERM
or a normal exit
Type: string | null
Default: utf8
Specify the character encoding used to decode the stdout
and stderr
output. If set to null
, then stdout
and stderr
will be a Buffer
instead of a string.
Type: number
Default: 0
If timeout is greater than 0
, the parent will send the signal identified by the killSignal
property (the default is SIGTERM
) if the child runs longer than timeout milliseconds.
Type: boolean
Default: true
Buffer the output from the spawned process. When buffering is disabled you must consume the output of the stdout
and stderr
streams because the promise will not be resolved/rejected until they have completed.
Type: number
Default: 10000000
(10MB)
Largest amount of data in bytes allowed on stdout
or stderr
.
Type: string | number
Default: SIGTERM
Signal value to be used when the spawned process will be killed.
Type: string | number | Stream | undefined
Default: pipe
Same options as stdio
.
Type: string | number | Stream | undefined
Default: pipe
Same options as stdio
.
Type: string | number | Stream | undefined
Default: pipe
Same options as stdio
.
Type: boolean
Default: false
If true
, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to true
automatically when the shell
option is true
.
Let's say you want to show the output of a child process in real-time while also saving it to a variable.
const execa = require('execa');
const getStream = require('get-stream');
const stream = execa('echo', ['foo']).stdout;
stream.pipe(process.stdout);
getStream(stream).then(value => {
console.log('child output:', value);
});