1 # execa [![Build Status: Linux](https://travis-ci.org/sindresorhus/execa.svg?branch=master)](https://travis-ci.org/sindresorhus/execa) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/x5ajamxtjtt93cqv/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/execa/branch/master) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/execa/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/execa?branch=master)
3 > A better [`child_process`](https://nodejs.org/api/child_process.html)
9 - [Strips EOF](https://github.com/sindresorhus/strip-eof) from the output so you don't have to `stdout.trim()`.
10 - Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.
11 - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)
12 - Higher max buffer. 10 MB instead of 200 KB.
13 - [Executes locally installed binaries by name.](#preferlocal)
14 - [Cleans up spawned processes when the parent process dies.](#cleanup)
20 $ npm install --save execa
27 const execa = require('execa');
29 execa('echo', ['unicorns']).then(result => {
30 console.log(result.stdout);
34 // pipe the child process stdout to the current stdout
35 execa('echo', ['unicorns']).stdout.pipe(process.stdout);
37 execa.shell('echo unicorns').then(result => {
38 console.log(result.stdout);
42 // example of catching an error
43 execa.shell('exit 3').catch(error => {
47 message: 'Command failed: /bin/sh -c exit 3'
51 cmd: '/bin/sh -c exit 3',
63 ### execa(file, [arguments], [options])
67 Think of this as a mix of `child_process.execFile` and `child_process.spawn`.
69 Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.
71 ### execa.stdout(file, [arguments], [options])
73 Same as `execa()`, but returns only `stdout`.
75 ### execa.stderr(file, [arguments], [options])
77 Same as `execa()`, but returns only `stderr`.
79 ### execa.shell(command, [options])
81 Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.
83 Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).
85 The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.
87 ### execa.sync(file, [arguments], [options])
89 Execute a file synchronously.
91 Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
93 This method throws an `Error` if the command fails.
95 ### execa.shellSync(file, [options])
97 Execute a command synchronously through the system shell.
99 Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
108 Default: `process.cwd()`
110 Current working directory of the child process.
115 Default: `process.env`
117 Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.
124 Set to `false` if you don't want to extend the environment variables when providing the `env` property.
130 Explicitly set the value of `argv[0]` sent to the child process. This will be set to `command` or `file` if not specified.
134 Type: `Array` `string`<br>
137 Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
143 Prepare child to run independently of its parent process. Specific behavior [depends on the platform](https://nodejs.org/api/child_process.html#child_process_options_detached).
149 Sets the user identity of the process.
155 Sets the group identity of the process.
159 Type: `boolean` `string`<br>
162 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.
169 [Strip EOF](https://github.com/sindresorhus/strip-eof) (last newline) from the output.
176 Prefer locally installed binaries when looking for a binary to execute.<br>
177 If you `$ npm install foo`, you can then `execa('foo')`.
182 Default: `process.cwd()`
184 Preferred path to find locally installed binaries in (use with `preferLocal`).
188 Type: `string` `Buffer` `stream.Readable`
190 Write some input to the `stdin` of your binary.<br>
191 Streams are not allowed when using the synchronous methods.
198 Setting this to `false` resolves the promise with the error instead of rejecting it.
205 Keep track of the spawned process and `kill` it when the parent process exits.
212 Specify the character encoding used to decode the `stdout` and `stderr` output.
219 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.
224 Default: `10000000` (10MB)
226 Largest amount of data in bytes allowed on `stdout` or `stderr`.
230 Type: `string` `number`<br>
233 Signal value to be used when the spawned process will be killed.
237 Type: `string` `number` `Stream` `undefined` `null`<br>
240 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
244 Type: `string` `number` `Stream` `undefined` `null`<br>
247 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
251 Type: `string` `number` `Stream` `undefined` `null`<br>
254 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
259 ### Save and pipe output from a child process
261 Let's say you want to show the output of a child process in real-time while also saving it to a variable.
264 const execa = require('execa');
265 const getStream = require('get-stream');
267 const stream = execa('echo', ['foo']).stdout;
269 stream.pipe(process.stdout);
271 getStream(stream).then(value => {
272 console.log('child output:', value);
279 MIT © [Sindre Sorhus](https://sindresorhus.com)