github.com/sindresorhus/execa
- Folder:.github
- Folder:docs
- Folder:lib
- Folder:media
- Folder:test
- Folder:test-d
- Folder:types
- File:.editorconfig
- File:.gitattributes
- File:.gitignore
- File:.npmrc
- File:index.d.ts
- File:index.js
- File:license
- File:package.json
- File:readme.md
readme.md
Process execution for humans
Why
This package improves child_process methods with:
- Promise interface.
- Script interface and template strings, like
zx. - Improved Windows support, including shebang binaries.
- Executes locally installed binaries without
npx. - Cleans up subprocesses when the current process ends.
- Redirect
stdin/stdout/stderrfrom/to files, streams, iterables, strings,Uint8Arrayor objects. - Transform
stdin/stdout/stderrwith simple functions. - Iterate over each text line output by the subprocess.
- Fail-safe subprocess termination.
- Get interleaved output from
stdoutandstderrsimilar to what is printed on the terminal. - Strips the final newline from the output so you don't have to do
stdout.trim(). - Convenience methods to pipe multiple subprocesses.
- Verbose mode for debugging.
- More descriptive errors.
- Higher max buffer: 100 MB instead of 1 MB.
Install
npm install execa
Documentation
Execution:
- โถ๏ธ Basic execution
- ๐ฌ Escaping/quoting
- ๐ป Shell
- ๐ Scripts
- ๐ข Node.js files
- ๐ Environment
- โ Errors
- ๐ Termination
Input/output:
- ๐น Input
- ๐ข Output
- ๐ Text lines
- ๐ค Binary data
- ๐ง Transforms
Advanced usage:
- ๐ Piping multiple subprocesses
- โณ๏ธ Streams
- ๐ Inter-process communication
- ๐ Debugging
- ๐ Windows
- ๐ Difference with Bash and zx
- ๐ API reference
Usage
Promise interface
import {execa} from 'execa';
const {stdout} = await execa('echo', ['unicorns']);
console.log(stdout);
//=> 'unicorns'
Global/shared options
import {execa as execa_} from 'execa';
const execa = execa_({verbose: 'full'});
await execa('echo', ['unicorns']);
//=> 'unicorns'
Template string syntax
Basic
import {execa} from 'execa';
const arg = 'unicorns';
const {stdout} = await execa`echo ${arg} & rainbows!`;
console.log(stdout);
//=> 'unicorns & rainbows!'
Multiple arguments
import {execa} from 'execa';
const args = ['unicorns', '&', 'rainbows!'];
const {stdout} = await execa`echo ${args}`;
console.log(stdout);
//=> 'unicorns & rainbows!'
With options
import {execa} from 'execa';
await execa({verbose: 'full'})`echo unicorns`;
//=> 'unicorns'
Scripts
Basic
import {$} from 'execa';
const branch = await $`git branch --show-current`;
await $`dep deploy --branch=${branch}`;
Verbose mode
> node file.js
unicorns
rainbows
> NODE_DEBUG=execa node file.js
[19:49:00.360] [0] $ echo unicorns
unicorns
[19:49:00.383] [0] โ (done in 23ms)
[19:49:00.383] [1] $ echo rainbows
rainbows
[19:49:00.404] [1] โ (done in 21ms)
Input/output
Redirect output to a file
import {execa} from 'execa';
// Similar to `echo unicorns > stdout.txt` in Bash
await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});
// Similar to `echo unicorns 2> stdout.txt` in Bash
await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});
// Similar to `echo unicorns &> stdout.txt` in Bash
await execa('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 Bash
const {stdout} = await execa('cat', {inputFile: 'stdin.txt'});
console.log(stdout);
//=> 'unicorns'
Save and pipe output from a subprocess
import {execa} from 'execa';
const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});
// Prints `unicorns`
console.log(stdout);
// Also returns 'unicorns'
Pipe multiple subprocesses
import {execa} from 'execa';
// Similar to `npm run build | sort | head -n2` in Bash
const {stdout, pipedFrom} = await execa('npm', ['run', 'build'])
.pipe('sort')
.pipe('head', ['-n2']);
console.log(stdout); // Result of `head -n2`
console.log(pipedFrom[0]); // Result of `sort`
console.log(pipedFrom[0].pipedFrom[0]); // Result of `npm run build`
Pipe with template strings
import {execa} from 'execa';
await execa`npm run build`
.pipe`sort`
.pipe`head -n2`;
Iterate over output lines
import {execa} from 'execa';
for await (const line of execa`npm run build`)) {
if (line.includes('ERROR')) {
console.log(line);
}
}
Handling Errors
import {execa} from 'execa';
// Catching an error
try {
await execa('unknown', ['command']);
} catch (error) {
console.log(error);
/*
ExecaError: Command failed with ENOENT: unknown command
spawn unknown ENOENT
at ...
at ... {
shortMessage: 'Command failed with ENOENT: unknown command\nspawn unknown ENOENT',
originalMessage: 'spawn unknown ENOENT',
command: 'unknown command',
escapedCommand: 'unknown command',
cwd: '/path/to/cwd',
durationMs: 28.217566,
failed: true,
timedOut: false,
isCanceled: false,
isTerminated: false,
isMaxBuffer: false,
code: 'ENOENT',
stdout: '',
stderr: '',
stdio: [undefined, '', ''],
pipedFrom: []
[cause]: Error: spawn unknown ENOENT
at ...
at ... {
errno: -2,
code: 'ENOENT',
syscall: 'spawn unknown',
path: 'unknown',
spawnargs: [ 'command' ]
}
}
*/
}
Related
- gulp-execa - Gulp plugin for Execa
- nvexeca - Run Execa using any Node.js version