Execa

Process execution for humans

README

execa logo
Coverage Status

Process execution for humans


Why


This package improves [child_process](https://nodejs.org/api/child_process.html) methods with:

- Promise interface.
- Strips the final newline from the output so you don't have to dostdout.trim().
- Supports shebang) binaries cross-platform.
- Higher max buffer. 100 MB instead of 200 KB.
- Get interleaved output fromstdout and stderr similar to what is printed on the terminal. [(Async only)](#execasyncfile-arguments-options)
- More descriptive errors.

Install


  1. ```sh
  2. npm install execa
  3. ```

Usage


  1. ```js
  2. import {execa} from 'execa';

  3. const {stdout} = await execa('echo', ['unicorns']);
  4. console.log(stdout);
  5. //=> 'unicorns'
  6. ```

Pipe the child process stdout to the parent


  1. ```js
  2. import {execa} from 'execa';

  3. execa('echo', ['unicorns']).stdout.pipe(process.stdout);
  4. ```

Handling Errors


  1. ```js
  2. import {execa} from 'execa';

  3. // Catching an error
  4. try {
  5. await execa('unknown', ['command']);
  6. } catch (error) {
  7. console.log(error);
  8. /*
  9. {
  10.   message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  11.   errno: -2,
  12.   code: 'ENOENT',
  13.   syscall: 'spawn unknown',
  14.   path: 'unknown',
  15.   spawnargs: ['command'],
  16.   originalMessage: 'spawn unknown ENOENT',
  17.   shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  18.   command: 'unknown command',
  19.   escapedCommand: 'unknown command',
  20.   stdout: '',
  21.   stderr: '',
  22.   all: '',
  23.   failed: true,
  24.   timedOut: false,
  25.   isCanceled: false,
  26.   killed: false
  27. }
  28. */
  29. }
  30. ```

Cancelling a spawned process


  1. ```js
  2. import {execa} from 'execa';

  3. const abortController = new AbortController();
  4. const subprocess = execa('node', [], {signal: abortController.signal});

  5. setTimeout(() => {
  6. abortController.abort();
  7. }, 1000);

  8. try {
  9. await subprocess;
  10. } catch (error) {
  11. console.log(subprocess.killed); // true
  12. console.log(error.isCanceled); // true
  13. }
  14. ```

Catching an error with the sync method


  1. ```js
  2. import {execaSync} from 'execa';

  3. try {
  4. execaSync('unknown', ['command']);
  5. } catch (error) {
  6. console.log(error);
  7. /*
  8. {
  9.   message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
  10.   errno: -2,
  11.   code: 'ENOENT',
  12.   syscall: 'spawnSync unknown',
  13.   path: 'unknown',
  14.   spawnargs: ['command'],
  15.   originalMessage: 'spawnSync unknown ENOENT',
  16.   shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',
  17.   command: 'unknown command',
  18.   escapedCommand: 'unknown command',
  19.   stdout: '',
  20.   stderr: '',
  21.   all: '',
  22.   failed: true,
  23.   timedOut: false,
  24.   isCanceled: false,
  25.   killed: false
  26. }
  27. */
  28. }
  29. ```

Kill a process


Using SIGTERM, and after 2 seconds, kill it with SIGKILL.

  1. ```js
  2. const subprocess = execa('node');

  3. setTimeout(() => {
  4. subprocess.kill('SIGTERM', {
  5.   forceKillAfterTimeout: 2000
  6. });
  7. }, 1000);
  8. ```

API


execa(file, arguments, options?)


Execute a file. Think of this as a mix of [child_process.execFile()](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback) and [child_process.spawn()](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).

No escaping/quoting is needed.

Unless the [shell](#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](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which:
  - is also a Promise resolving or rejecting with a [childProcessResult](#childProcessResult).
  - exposes the following additional methods and properties.

kill(signal?, options?)


Same as the original [child_process#kill()](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal) except: if signal is SIGTERM (the default value) and the child process is not terminated after 5 seconds, force it by sending SIGKILL.

options.forceKillAfterTimeout

Type: number | false\
Default: 5000

Milliseconds to wait for the child process to terminate before sending SIGKILL.

Can be disabled with false.

all


Type: ReadableStream | undefined

Stream combining/interleaving [stdout](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [stderr](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).

This is undefined if either:
  - the [all option](#all-2) is false (the default value)
  - both [stdout](#stdout-1) and [stderr](#stderr-1) options are set to ['inherit', 'ipc', Stream or integer](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)

execaSync(file, arguments?, options?)


Execute a file synchronously.

Returns or throws a [childProcessResult](#childProcessResult).

execaCommand(command, options?)


Same as [execa()](#execafile-arguments-options) except both file and arguments are specified in a single command string. For example, execa('echo', ['unicorns']) is the same as execaCommand('echo unicorns').

If the file or an argument contains spaces, they must be escaped with backslashes. This matters especially if command is not a constant but a variable, for example with __dirname or process.cwd(). Except for spaces, no escaping/quoting is needed.

The [shell option](#shell) must be used if the command uses shell-specific features (for example, && or ||), as opposed to being a simple file followed by its arguments.

execaCommandSync(command, options?)


Same as [execaCommand()](#execacommand-command-options) but synchronous.

Returns or throws a [childProcessResult](#childProcessResult).

execaNode(scriptPath, arguments?, options?)


Execute a Node.js script as a child process.

Same as execa('node', [scriptPath, ...arguments], options) except (like [child_process#fork()](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)):
  - the current Node version and options are used. This can be overridden using the [nodePath](#nodepath-for-node-only) and [nodeOptions](#nodeoptions-for-node-only) options.
  - the [shell](#shell) option cannot be used
  - an extra channel [ipc](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [stdio](#stdio)

childProcessResult


Type: object

Result of a child process execution. On success this is a plain object. On failure this is also an Error instance.

The child process fails when:
- its exit code is not0
- it was killed with a signal
- there's not enough memory or there are already too many child processes

command


Type: string

The file and arguments that were run, for logging purposes.

This is not escaped and should not be executed directly as a process, including using [execa()](#execafile-arguments-options) or [execaCommand()](#execacommandcommand-options).

escapedCommand


Type: string

Same as [command](#command) but escaped.

This is meant to be copy and pasted into a shell, for debugging purposes.
Since the escaping is fairly basic, this should not be executed directly as a process, including using [execa()](#execafile-arguments-options) or [execaCommand()](#execacommandcommand-options).

exitCode


Type: number

The numeric exit code of the process that was run.

stdout


Type: string | Buffer

The output of the process on stdout.

stderr


Type: string | Buffer

The output of the process on stderr.

all


Type: string | Buffer | undefined

The output of the process with stdout and stderr interleaved.

This is undefined if either:
  - the [all option](#all-2) is false (the default value)
  - execaSync() was used

failed


Type: boolean

Whether the process failed to run.

timedOut


Type: boolean

Whether the process timed out.

isCanceled


Type: boolean

Whether the process was canceled.

You can cancel the spawned process using the [signal](#signal-1) option.

killed


Type: boolean

Whether the process was killed.

signal


Type: string | undefined

The name of the signal that was used to terminate the process. For example, SIGFPE.

If a signal terminated the process, this property is defined and included in the error message. Otherwise it is undefined.

signalDescription


Type: string | undefined

A human-friendly description of the signal that was used to terminate the process. For example, Floating point arithmetic error.

If a signal terminated the process, this property is defined and included in the error message. Otherwise it is undefined. It is also undefined when the signal is very uncommon which should seldomly happen.

message


Type: string

Error message when the child process failed to run. In addition to the underlying error message, it also contains some information related to why the child process errored.

The child process stderr then stdout are appended to the end, separated with newlines and not interleaved.

shortMessage


Type: string

This is the same as the [message property](#message) except it does not include the child process stdout/stderr.

originalMessage


Type: string | undefined

Original error message. This is the same as the message property except it includes neither the child process stdout/stderr nor some additional information added by Execa.

This is undefined unless the child process exited due to an error event or a timeout.

options


Type: object

cleanup


Type: boolean\
Default: true

Kill the spawned process when the parent process exits unless either:
- the spawned process is [detached](https://nodejs.org/api/child_process.html#child_process_options_detached)
- the parent process is terminated abruptly, for example, with SIGKILL as opposed to SIGTERM or a normal exit

preferLocal


Type: boolean\
Default: false

Prefer locally installed binaries when looking for a binary to execute.\
If you $ npm install foo, you can then execa('foo').

localDir


Type: string | URL\
Default: process.cwd()

Preferred path to find locally installed binaries in (use with preferLocal).

execPath


Type: string\
Default: process.execPath (Current Node.js executable)

Path to the Node.js executable to use in child processes.

This can be either an absolute path or a path relative to the [cwd option](#cwd).

Requires [preferLocal](#preferlocal) to be true.

For example, this can be used together with [get-node](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.

buffer


Type: boolean\
Default: true

Buffer the output from the spawned process. When set to false, you must read the output of [stdout](#stdout-1) and [stderr](#stderr-1) (or [all](#all) if the [all](#all-2) option is true). Otherwise the returned promise will not be resolved/rejected.

If the spawned process fails, [error.stdout](#stdout), [error.stderr](#stderr), and [error.all](#all) will contain the buffered data.

input


Type: string | Buffer | stream.Readable

Write some input to the stdin of your binary.\
Streams are not allowed when using the synchronous methods.

stdin


Type: string | number | Stream | undefined\
Default: pipe

Same options as [stdio](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).

stdout


Type: string | number | Stream | undefined\
Default: pipe

Same options as [stdio](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).

stderr


Type: string | number | Stream | undefined\
Default: pipe

Same options as [stdio](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).

all


Type: boolean\
Default: false

Add an .all property on the promise and the resolved value. The property contains the output of the process withstdout and stderr interleaved.

reject


Type: boolean\
Default: true

Setting this to false resolves the promise with the error instead of rejecting it.

stripFinalNewline


Type: boolean\
Default: true

Strip the final newline character from the output.

extendEnv


Type: boolean\
Default: true

Set to false if you don't want to extend the environment variables when providing the env property.


Execa also accepts the below options which are the same as the options for [child_process#spawn()](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)/[child_process#exec()](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)

cwd


Type: string | URL\
Default: process.cwd()

Current working directory of the child process.

env


Type: object\
Default: process.env

Environment key-value pairs. Extends automatically from process.env. Set [extendEnv](#extendenv) to false if you don't want this.

argv0


Type: string

Explicitly set the value of argv[0] sent to the child process. This will be set to file if not specified.

stdio


Type: string | string[]\
Default: pipe

Child's stdio configuration.

serialization


Type: string\
Default: 'json'

Specify the kind of serialization used for sending messages between processes when using the [stdio: 'ipc'](#stdio) option or [execaNode()](#execanodescriptpath-arguments-options):
- json: Uses JSON.stringify() and JSON.parse().
- advanced: Uses [v8.serialize()](https://nodejs.org/api/v8.html#v8_v8_serialize_value)


detached


Type: boolean

Prepare child to run independently of its parent process. Specific behavior depends on the platform.

uid


Type: number

Sets the user identity of the process.

gid


Type: number

Sets the group identity of the process.

shell


Type: boolean | string\
Default: false

If true, runs file 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.

encoding


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.

timeout


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.

maxBuffer


Type: number\
Default: 100_000_000 (100 MB)

Largest amount of data in bytes allowed on stdout or stderr.

killSignal


Type: string | number\
Default: SIGTERM

Signal value to be used when the spawned process will be killed.

signal


Type: [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

You can abort the spawned process using [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).

When AbortController.abort() is called, [.isCanceled](#iscanceled) becomes false.

Requires Node.js 16 or later.

windowsVerbatimArguments


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.

windowsHide


Type: boolean\
Default: true

On Windows, do not create a new console window. Please note this also prevents CTRL-C from working on Windows.

nodePath (For .node() only)


Type: string\
Default: [process.execPath](https://nodejs.org/api/process.html#process_process_execpath)

Node.js executable used to create the child process.

nodeOptions (For .node() only)


Type: string[]\
Default: [process.execArgv](https://nodejs.org/api/process.html#process_process_execargv)

List of CLI options passed to the Node.js executable.

Tips


Retry on error


Gracefully handle failures by using automatic retries and exponential backoff with the [p-retry](https://github.com/sindresorhus/p-retry) package:

  1. ```js
  2. import pRetry from 'p-retry';

  3. const run = async () => {
  4. const results = await execa('curl', ['-sSL', 'https://sindresorhus.com/unicorn']);
  5. return results;
  6. };

  7. console.log(await pRetry(run, {retries: 5}));
  8. ```

Save and pipe output from a child process


Let's say you want to show the output of a child process in real-time while also saving it to a variable.

  1. ```js
  2. import {execa} from 'execa';

  3. const subprocess = execa('echo', ['foo']);
  4. subprocess.stdout.pipe(process.stdout);

  5. const {stdout} = await subprocess;
  6. console.log('child output:', stdout);
  7. ```

Redirect output to a file


  1. ```js
  2. import {execa} from 'execa';

  3. const subprocess = execa('echo', ['foo'])
  4. subprocess.stdout.pipe(fs.createWriteStream('stdout.txt'))
  5. ```

Redirect input from a file


  1. ```js
  2. import {execa} from 'execa';

  3. const subprocess = execa('cat')
  4. fs.createReadStream('stdin.txt').pipe(subprocess.stdin)
  5. ```

Execute the current package's binary


  1. ```js
  2. import {getBinPathSync} from 'get-bin-path';

  3. const binPath = getBinPathSync();
  4. const subprocess = execa(binPath);
  5. ```

execa can be combined with [get-bin-path](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the package.json bin field is correctly set up.

Related


- gulp-execa - Gulp plugin forexeca
- nvexeca - Runexeca using any Node.js version
- sudo-prompt - Run commands with elevated privileges.

Maintainers




Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.