What is execa?
The execa npm package is a process execution tool that simplifies working with child processes in Node.js. It provides a better user experience than the default child_process module by offering a promise-based API, improved Windows support, and additional convenience options.
What are execa's main functionalities?
Executing a shell command
This feature allows you to execute a shell command and obtain the result. The example shows how to execute the 'echo' command and print 'unicorns' to the console.
const execa = require('execa');
(async () => {
const { stdout } = await execa('echo', ['unicorns']);
console.log(stdout);
})();
Running a command synchronously
This feature is used to execute a command synchronously, blocking the event loop until the process has finished. The example synchronously executes the 'echo' command and logs the result.
const execa = require('execa');
const { stdout } = execa.sync('echo', ['unicorns']);
console.log(stdout);
Handling errors
This feature demonstrates error handling when a command fails to execute. The example attempts to run a non-existent command and catches the error.
const execa = require('execa');
(async () => {
try {
const { stdout } = await execa('wrong-command');
console.log(stdout);
} catch (error) {
console.error('Error occurred:', error);
}
})();
Streaming output
This feature allows you to stream the output of a command directly to the console or another stream. The example streams the output of the 'echo' command to the process's stdout.
const execa = require('execa');
const subprocess = execa('echo', ['unicorns']);
subprocess.stdout.pipe(process.stdout);
Other packages similar to execa
shelljs
ShellJS is a portable Unix shell commands implementation for Node.js. It offers a higher-level API for executing commands but does not support returning promises natively.
cross-spawn
Cross-spawn is a cross-platform solution for spawning child processes. It aims to solve compatibility issues on Windows but does not provide a promise-based API.
execa
A better child_process
Why
Install
$ npm install --save execa
Usage
const execa = require('execa');
execa('echo', ['unicorns']).then(result => {
console.log(result.stdout);
});
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
execa.shell('echo unicorns').then(result => {
console.log(result.stdout);
});
execa.shell('exit 3').catch(error => {
console.log(error);
});
API
execa(file, [arguments], [options])
Execute a file.
Same options as child_process.spawn
.
Think of this as a mix of child_process.execFile
and child_process.spawn
.
Returns a child_process
instance, which is enhanced to also be a Promise
for a result Object
with stdout
and stderr
properties.
execa.stdout(file, [arguments], [options])
Same as execa()
, but returns only stdout
.
execa.stderr(file, [arguments], [options])
Same as execa()
, but returns only stderr
.
execa.shell(command, [options])
Execute a command through the system shell. Prefer execa()
whenever possible, as it's both faster and safer.
Same options as child_process.spawn
.
Returns a child_process
instance.
The child_process
instance is enhanced to also be promise for a result object with stdout
and stderr
properties.
execa.sync(file, [arguments], [options])
Execute a file synchronously.
Same options as child_process.spawnSync
, except the default encoding is utf8
instead of buffer
.
Returns the same result object as child_process.spawnSync
.
This method throws an Error
if the command fails.
execa.shellSync(file, [options])
Execute a command synchronously through the system shell.
Same options as child_process.spawnSync
, except the default encoding is utf8
instead of buffer
.
Returns the same result object as child_process.spawnSync
.
options
Additional options:
stripEof
Type: boolean
Default: true
Strip EOF (last newline) from the output.
preferLocal
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')
.
input
Type: string
Buffer
ReadableStream
Write some input to the stdin
of your binary.
Streams are not allowed when using the synchronous methods.
reject
Type: boolean
Default: true
Setting this to false
resolves the promise with the error instead of rejecting it.
cleanup
Type: boolean
Default: true
Keep track of the spawned process and kill
it when the parent process exits.
Tips
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.
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);
});
License
MIT © Sindre Sorhus