import {execa} from'execa';
// Similar to `echo unicorns > stdout.txt` in Bashawaitexeca('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});
// Similar to `echo unicorns 2> stdout.txt` in Bashawaitexeca('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});
// Similar to `echo unicorns &> stdout.txt` in Bashawaitexeca('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});
Redirect input from a file
import {execa} from'execa';
// Similar to `cat < stdin.txt` in Bashconst {stdout} = awaitexeca('cat', {inputFile: 'stdin.txt'});
console.log(stdout);
//=> 'unicorns'
Save and pipe output from a child process
import {execa} from'execa';
const {stdout} = awaitexeca('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});
// Prints `unicorns`console.log(stdout);
// Also returns 'unicorns'
Pipe multiple processes
import {execa} from'execa';
// Similar to `echo unicorns | cat` in Bashconst {stdout} = awaitexeca('echo', ['unicorns']).pipe(execa('cat'));
console.log(stdout);
//=> 'unicorns'
Executes a command using file ...arguments. file is a string or a file URL. arguments are an array of strings. Returns a childProcess.
Arguments are automatically escaped. They can contain any character, including spaces.
This is the preferred method when executing single commands.
$`command`
Executes a command. The command string includes both the file and its arguments. Returns a childProcess.
Arguments are automatically escaped. They can contain any character, but spaces must use ${} like $`echo ${'has space'}`.
This is the preferred method when executing multiple commands in a script file.
The command string can inject any ${value} with the following types: string, number, childProcess or an array of those types. For example: $`echo one ${'two'} ${3} ${['four', 'five']}`. For ${childProcess}, the process's stdout is used.
Executes a command. The command string includes both the file and its arguments. Returns a childProcess.
Arguments are automatically escaped. They can contain any character, but spaces must be escaped with a backslash like execaCommand('echo has\\ space').
This is the preferred method when executing a user-supplied command string, such as in a REPL.
For all the methods above, no shell interpreter (Bash, cmd.exe, etc.) is used unless the shell option is set. This means shell-specific characters and expressions ($variable, &&, ||, ;, |, etc.) have no special meaning and do not need to be escaped.
Returns execaChildProcess, which allows chaining .pipe() then awaiting the final result.
kill(signalOrError?)
signalOrError: string | number | Error Returns: boolean
Sends a signal to the child process. The default signal is the killSignal option. killSignal defaults to SIGTERM, which terminates the child process.
This returns false when the signal could not be sent, for example when the child process has already exited.
When an error is passed as argument, its message and stack trace are kept in the child process' error. The child process is then terminated with the default signal. This does not emit the error event.
This is meant to be copied 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() or execaCommand().
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.
Original error message. This is the same as the message property excluding the child process stdout/stderr/stdio and some additional information added by Execa.
This is undefined unless the child process exited due to an error event or a timeout.
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 using the signal option.
isTerminated
Type: boolean
Whether the process was terminated by a signal (like SIGTERM) sent by either:
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.
Setting this to false resolves the promise with the error instead of rejecting it.
shell
Type: boolean | string | URL
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.
cwd
Type: string | URL
Default: process.cwd()
Current working directory of the child process.
This is also used to resolve the nodePath option when it is a relative path.
env
Type: object
Default: process.env
Environment key-value pairs.
Unless the extendEnv option is false, the child process also uses the current process' environment variables (process.env).
extendEnv
Type: boolean
Default: true
If true, the child process uses both the env option and the current process' environment variables (process.env).
If false, only the env option is used, not process.env.
preferLocal
Type: boolean
Default: true with $, false otherwise
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).
node
Type: boolean
Default: true with execaNode(), false otherwise
If true, runs with Node.js. The first argument must be a Node.js file.
When the node option is true, this is used to to create the child process. When the preferLocal option is true, this is used in the child process itself.
For example, this can be used together with get-node to run a specific Node.js version.
Like the stdin, stdout and stderr options but for all file descriptors at once. For example, {stdio: ['ignore', 'pipe', 'pipe']} is the same as {stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}.
A single string can be used as a shortcut. For example, {stdio: 'pipe'} is the same as {stdin: 'pipe', stdout: 'pipe', stderr: 'pipe'}.
The array can have more than 3 items, to create additional file descriptors beyond stdin/stdout/stderr. For example, {stdio: ['pipe', 'pipe', 'pipe', 'pipe']} sets a fourth file descriptor.
all
Type: boolean
Default: false
Add an .all property on the promise and the resolved value. The property contains the output of the process with stdout and stderrinterleaved.
Any stream passed to the stdout, stderr or stdio option receives lines instead of arbitrary chunks.
encoding
Type: string
Default: utf8
Specify the character encoding used to decode the stdout, stderr and stdio output. If set to 'buffer', then stdout, stderr and stdio will be Uint8Arrays instead of strings.
Specify the kind of serialization used for sending messages between processes when using the ipc option:
- json: Uses JSON.stringify() and JSON.parse().
- advanced: Uses v8.serialize()
Prepare child to run independently of its parent process. Specific behavior depends on the platform.
cleanup
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
timeout
Type: number
Default: 0
If timeout is greater than 0, the child process will be terminated if it runs for longer than that amount of milliseconds.
Also, this does not work on Windows, because Windows doesn't support signals: SIGKILL and SIGTERM both terminate the process immediately. Other packages (such as taskkill) can be used to achieve fail-safe termination on Windows.
This can be either a name (like "SIGTERM") or a number (like 9).
argv0
Type: string
Explicitly set the value of argv[0] sent to the child process. This will be set to file if not specified.
uid
Type: number
Sets the user identity of the process.
gid
Type: number
Sets the group identity of the process.
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-Cfrom working on Windows.
Tips
Redirect stdin/stdout/stderr to multiple destinations
The stdin, stdout and stderr options can be an array of values.
The following example redirects stdout to both the terminal and an output.txt file, while also retrieving its value programmatically.
When combining inherit with other values, please note that the child process will not be an interactive TTY, even if the parent process is one.
Redirect a Node.js stream from/to stdin/stdout/stderr
When passing a Node.js stream to the stdin, stdout or stderr option, Node.js requires that stream to have an underlying file or socket, such as the streams created by the fs, net or http core modules. Otherwise the following error is thrown.
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'stdio' is invalid.
This limitation can be worked around by passing either:
Execa can be combined with 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.jsonbin field is correctly set up.
However, for performance reasons, the child process might buffer and merge multiple simultaneous writes to stdout or stderr. This prevents proper interleaving.
For example, this prints 1 3 2 instead of 1 2 3 because both console.log() are merged into a single write.
We found that @rebundled/execa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 15 Feb 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.