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)
23 <a href="https://www.patreon.com/sindresorhus">
24 <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
31 const execa = require('execa');
34 const {stdout} = await execa('echo', ['unicorns']);
43 const execa = require('execa');
46 // Pipe the child process stdout to the current stdout
47 execa('echo', ['unicorns']).stdout.pipe(process.stdout);
50 // Run a shell command
51 const {stdout} = await execa.shell('echo unicorns');
57 await execa.shell('exit 3');
62 message: 'Command failed: /bin/sh -c exit 3'
66 cmd: '/bin/sh -c exit 3',
75 // Catching an error with a sync method
77 execa.shellSync('exit 3');
82 message: 'Command failed: /bin/sh -c exit 3'
85 cmd: '/bin/sh -c exit 3',
97 ### execa(file, [arguments], [options])
101 Think of this as a mix of `child_process.execFile` and `child_process.spawn`.
103 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.
105 ### execa.stdout(file, [arguments], [options])
107 Same as `execa()`, but returns only `stdout`.
109 ### execa.stderr(file, [arguments], [options])
111 Same as `execa()`, but returns only `stderr`.
113 ### execa.shell(command, [options])
115 Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.
117 Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).
119 The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.
121 ### execa.sync(file, [arguments], [options])
123 Execute a file synchronously.
125 Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
127 This method throws an `Error` if the command fails.
129 ### execa.shellSync(file, [options])
131 Execute a command synchronously through the system shell.
133 Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
142 Default: `process.cwd()`
144 Current working directory of the child process.
149 Default: `process.env`
151 Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.
158 Set to `false` if you don't want to extend the environment variables when providing the `env` property.
164 Explicitly set the value of `argv[0]` sent to the child process. This will be set to `command` or `file` if not specified.
168 Type: `string[]` `string`<br>
171 Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
177 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).
183 Sets the user identity of the process.
189 Sets the group identity of the process.
193 Type: `boolean` `string`<br>
196 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.
203 [Strip EOF](https://github.com/sindresorhus/strip-eof) (last newline) from the output.
210 Prefer locally installed binaries when looking for a binary to execute.<br>
211 If you `$ npm install foo`, you can then `execa('foo')`.
216 Default: `process.cwd()`
218 Preferred path to find locally installed binaries in (use with `preferLocal`).
222 Type: `string` `Buffer` `stream.Readable`
224 Write some input to the `stdin` of your binary.<br>
225 Streams are not allowed when using the synchronous methods.
232 Setting this to `false` resolves the promise with the error instead of rejecting it.
239 Keep track of the spawned process and `kill` it when the parent process exits.
246 Specify the character encoding used to decode the `stdout` and `stderr` output.
253 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.
260 Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.
265 Default: `10000000` (10MB)
267 Largest amount of data in bytes allowed on `stdout` or `stderr`.
271 Type: `string` `number`<br>
274 Signal value to be used when the spawned process will be killed.
278 Type: `string` `number` `Stream` `undefined` `null`<br>
281 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
285 Type: `string` `number` `Stream` `undefined` `null`<br>
288 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
292 Type: `string` `number` `Stream` `undefined` `null`<br>
295 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
297 #### windowsVerbatimArguments
302 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`.
307 ### Save and pipe output from a child process
309 Let's say you want to show the output of a child process in real-time while also saving it to a variable.
312 const execa = require('execa');
313 const getStream = require('get-stream');
315 const stream = execa('echo', ['foo']).stdout;
317 stream.pipe(process.stdout);
319 getStream(stream).then(value => {
320 console.log('child output:', value);
327 MIT © [Sindre Sorhus](https://sindresorhus.com)