\begin{quote}
A better \href{https://nodejs.org/api/child_process.html}{\texttt{ {\ttfamily child\+\_\+process}}} \end{quote}
\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9360}{}\doxysection{Why}\label{md_sfcode_node_modules_execa_readme_autotoc_md9360}

\begin{DoxyItemize}
\item Promise interface.
\item \href{https://github.com/sindresorhus/strip-eof}{\texttt{ Strips EOF}} from the output so you don\textquotesingle{}t have to {\ttfamily stdout.\+trim()}.
\item Supports \href{https://en.wikipedia.org/wiki/Shebang_(Unix)}{\texttt{ shebang}} binaries cross-\/platform.
\item \href{https://github.com/IndigoUnited/node-cross-spawn\#why}{\texttt{ Improved Windows support.}}
\item Higher max buffer. 10 MB instead of 200 KB.
\item \href{\#preferlocal}{\texttt{ Executes locally installed binaries by name.}}
\item \href{\#cleanup}{\texttt{ Cleans up spawned processes when the parent process dies.}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9361}{}\doxysection{Install}\label{md_sfcode_node_modules_execa_readme_autotoc_md9361}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install execa }

\end{DoxyCode}


\href{https://www.patreon.com/sindresorhus}{\texttt{  }}\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9362}{}\doxysection{Usage}\label{md_sfcode_node_modules_execa_readme_autotoc_md9362}

\begin{DoxyCode}{0}
\DoxyCodeLine{const execa = require('execa');}
\DoxyCodeLine{}
\DoxyCodeLine{(async () => \{}
\DoxyCodeLine{    const \{stdout\} = await execa('echo', ['unicorns']);}
\DoxyCodeLine{    console.log(stdout);}
\DoxyCodeLine{    //=> 'unicorns'}
\DoxyCodeLine{\})(); }

\end{DoxyCode}


Additional examples\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const execa = require('execa');}
\DoxyCodeLine{}
\DoxyCodeLine{(async () => \{}
\DoxyCodeLine{    // Pipe the child process stdout to the current stdout}
\DoxyCodeLine{    execa('echo', ['unicorns']).stdout.pipe(process.stdout);}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{    // Run a shell command}
\DoxyCodeLine{    const \{stdout\} = await execa.shell('echo unicorns');}
\DoxyCodeLine{    //=> 'unicorns'}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{    // Catching an error}
\DoxyCodeLine{    try \{}
\DoxyCodeLine{        await execa.shell('exit 3');}
\DoxyCodeLine{    \} catch (error) \{}
\DoxyCodeLine{        console.log(error);}
\DoxyCodeLine{        /*}
\DoxyCodeLine{        \{}
\DoxyCodeLine{            message: 'Command failed: /bin/sh -\/c exit 3'}
\DoxyCodeLine{            killed: false,}
\DoxyCodeLine{            code: 3,}
\DoxyCodeLine{            signal: null,}
\DoxyCodeLine{            cmd: '/bin/sh -\/c exit 3',}
\DoxyCodeLine{            stdout: '',}
\DoxyCodeLine{            stderr: '',}
\DoxyCodeLine{            timedOut: false}
\DoxyCodeLine{        \}}
\DoxyCodeLine{        */}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\})();}
\DoxyCodeLine{}
\DoxyCodeLine{// Catching an error with a sync method}
\DoxyCodeLine{try \{}
\DoxyCodeLine{    execa.shellSync('exit 3');}
\DoxyCodeLine{\} catch (error) \{}
\DoxyCodeLine{    console.log(error);}
\DoxyCodeLine{    /*}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        message: 'Command failed: /bin/sh -\/c exit 3'}
\DoxyCodeLine{        code: 3,}
\DoxyCodeLine{        signal: null,}
\DoxyCodeLine{        cmd: '/bin/sh -\/c exit 3',}
\DoxyCodeLine{        stdout: '',}
\DoxyCodeLine{        stderr: '',}
\DoxyCodeLine{        timedOut: false}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    */}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9363}{}\doxysection{API}\label{md_sfcode_node_modules_execa_readme_autotoc_md9363}
\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9364}{}\doxysubsection{execa(file, \mbox{[}arguments\mbox{]}, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9364}
Execute a file.

Think of this as a mix of {\ttfamily child\+\_\+process.\+exec\+File} and {\ttfamily child\+\_\+process.\+spawn}.

Returns a \href{https://nodejs.org/api/child_process.html\#child_process_class_childprocess}{\texttt{ {\ttfamily child\+\_\+process} instance}}, which is enhanced to also be a {\ttfamily Promise} for a result {\ttfamily Object} with {\ttfamily stdout} and {\ttfamily stderr} properties.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9365}{}\doxysubsection{execa.\+stdout(file, \mbox{[}arguments\mbox{]}, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9365}
Same as {\ttfamily execa()}, but returns only {\ttfamily stdout}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9366}{}\doxysubsection{execa.\+stderr(file, \mbox{[}arguments\mbox{]}, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9366}
Same as {\ttfamily execa()}, but returns only {\ttfamily stderr}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9367}{}\doxysubsection{execa.\+shell(command, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9367}
Execute a command through the system shell. Prefer {\ttfamily execa()} whenever possible, as it\textquotesingle{}s both faster and safer.

Returns a \href{https://nodejs.org/api/child_process.html\#child_process_class_childprocess}{\texttt{ {\ttfamily child\+\_\+process} instance}}.

The {\ttfamily child\+\_\+process} instance is enhanced to also be promise for a result object with {\ttfamily stdout} and {\ttfamily stderr} properties.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9368}{}\doxysubsection{execa.\+sync(file, \mbox{[}arguments\mbox{]}, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9368}
Execute a file synchronously.

Returns the same result object as \href{https://nodejs.org/api/child_process.html\#child_process_child_process_spawnsync_command_args_options}{\texttt{ {\ttfamily child\+\_\+process.\+spawn\+Sync}}}.

This method throws an {\ttfamily Error} if the command fails.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9369}{}\doxysubsection{execa.\+shell\+Sync(file, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_execa_readme_autotoc_md9369}
Execute a command synchronously through the system shell.

Returns the same result object as \href{https://nodejs.org/api/child_process.html\#child_process_child_process_spawnsync_command_args_options}{\texttt{ {\ttfamily child\+\_\+process.\+spawn\+Sync}}}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9370}{}\doxysubsection{options}\label{md_sfcode_node_modules_execa_readme_autotoc_md9370}
Type\+: {\ttfamily Object}\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9371}{}\doxysubsubsection{cwd}\label{md_sfcode_node_modules_execa_readme_autotoc_md9371}
Type\+: {\ttfamily string}~\newline
 Default\+: {\ttfamily process.\+cwd()}

Current working directory of the child process.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9372}{}\doxysubsubsection{env}\label{md_sfcode_node_modules_execa_readme_autotoc_md9372}
Type\+: {\ttfamily Object}~\newline
 Default\+: {\ttfamily process.\+env}

Environment key-\/value pairs. Extends automatically from {\ttfamily process.\+env}. Set {\ttfamily extend\+Env} to {\ttfamily false} if you don\textquotesingle{}t want this.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9373}{}\doxysubsubsection{extend\+Env}\label{md_sfcode_node_modules_execa_readme_autotoc_md9373}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Set to {\ttfamily false} if you don\textquotesingle{}t want to extend the environment variables when providing the {\ttfamily env} property.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9374}{}\doxysubsubsection{argv0}\label{md_sfcode_node_modules_execa_readme_autotoc_md9374}
Type\+: {\ttfamily string}

Explicitly set the value of {\ttfamily argv\mbox{[}0\mbox{]}} sent to the child process. This will be set to {\ttfamily command} or {\ttfamily file} if not specified.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9375}{}\doxysubsubsection{stdio}\label{md_sfcode_node_modules_execa_readme_autotoc_md9375}
Type\+: {\ttfamily string\mbox{[}\mbox{]}} {\ttfamily string}~\newline
 Default\+: {\ttfamily pipe}

Child\textquotesingle{}s \href{https://nodejs.org/api/child_process.html\#child_process_options_stdio}{\texttt{ stdio}} configuration.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9376}{}\doxysubsubsection{detached}\label{md_sfcode_node_modules_execa_readme_autotoc_md9376}
Type\+: {\ttfamily boolean}

Prepare child to run independently of its parent process. Specific behavior \href{https://nodejs.org/api/child_process.html\#child_process_options_detached}{\texttt{ depends on the platform}}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9377}{}\doxysubsubsection{uid}\label{md_sfcode_node_modules_execa_readme_autotoc_md9377}
Type\+: {\ttfamily number}

Sets the user identity of the process.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9378}{}\doxysubsubsection{gid}\label{md_sfcode_node_modules_execa_readme_autotoc_md9378}
Type\+: {\ttfamily number}

Sets the group identity of the process.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9379}{}\doxysubsubsection{shell}\label{md_sfcode_node_modules_execa_readme_autotoc_md9379}
Type\+: {\ttfamily boolean} {\ttfamily string}~\newline
 Default\+: {\ttfamily false}

If {\ttfamily true}, runs {\ttfamily command} inside of a shell. Uses {\ttfamily /bin/sh} on UNIX and {\ttfamily cmd.\+exe} on Windows. A different shell can be specified as a string. The shell should understand the {\ttfamily -\/c} switch on UNIX or {\ttfamily /d /s /c} on Windows.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9380}{}\doxysubsubsection{strip\+Eof}\label{md_sfcode_node_modules_execa_readme_autotoc_md9380}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

\href{https://github.com/sindresorhus/strip-eof}{\texttt{ Strip EOF}} (last newline) from the output.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9381}{}\doxysubsubsection{prefer\+Local}\label{md_sfcode_node_modules_execa_readme_autotoc_md9381}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Prefer locally installed binaries when looking for a binary to execute.~\newline
 If you {\ttfamily \$ npm install foo}, you can then `execa(\textquotesingle{}foo')\`{}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9382}{}\doxysubsubsection{local\+Dir}\label{md_sfcode_node_modules_execa_readme_autotoc_md9382}
Type\+: {\ttfamily string}~\newline
 Default\+: {\ttfamily process.\+cwd()}

Preferred path to find locally installed binaries in (use with {\ttfamily prefer\+Local}).\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9383}{}\doxysubsubsection{input}\label{md_sfcode_node_modules_execa_readme_autotoc_md9383}
Type\+: {\ttfamily string} {\ttfamily Buffer} {\ttfamily stream.\+Readable}

Write some input to the {\ttfamily stdin} of your binary.~\newline
 Streams are not allowed when using the synchronous methods.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9384}{}\doxysubsubsection{reject}\label{md_sfcode_node_modules_execa_readme_autotoc_md9384}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Setting this to {\ttfamily false} resolves the promise with the error instead of rejecting it.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9385}{}\doxysubsubsection{cleanup}\label{md_sfcode_node_modules_execa_readme_autotoc_md9385}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Keep track of the spawned process and {\ttfamily kill} it when the parent process exits.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9386}{}\doxysubsubsection{encoding}\label{md_sfcode_node_modules_execa_readme_autotoc_md9386}
Type\+: {\ttfamily string}~\newline
 Default\+: {\ttfamily utf8}

Specify the character encoding used to decode the {\ttfamily stdout} and {\ttfamily stderr} output.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9387}{}\doxysubsubsection{timeout}\label{md_sfcode_node_modules_execa_readme_autotoc_md9387}
Type\+: {\ttfamily number}~\newline
 Default\+: {\ttfamily 0}

If timeout is greater than {\ttfamily 0}, the parent will send the signal identified by the {\ttfamily kill\+Signal} property (the default is {\ttfamily SIGTERM}) if the child runs longer than timeout milliseconds.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9388}{}\doxysubsubsection{buffer}\label{md_sfcode_node_modules_execa_readme_autotoc_md9388}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Buffer the output from the spawned process. When buffering is disabled you must consume the output of the {\ttfamily stdout} and {\ttfamily stderr} streams because the promise will not be resolved/rejected until they have completed.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9389}{}\doxysubsubsection{max\+Buffer}\label{md_sfcode_node_modules_execa_readme_autotoc_md9389}
Type\+: {\ttfamily number}~\newline
 Default\+: {\ttfamily 10000000} (10MB)

Largest amount of data in bytes allowed on {\ttfamily stdout} or {\ttfamily stderr}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9390}{}\doxysubsubsection{kill\+Signal}\label{md_sfcode_node_modules_execa_readme_autotoc_md9390}
Type\+: {\ttfamily string} {\ttfamily number}~\newline
 Default\+: {\ttfamily SIGTERM}

Signal value to be used when the spawned process will be killed.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9391}{}\doxysubsubsection{stdin}\label{md_sfcode_node_modules_execa_readme_autotoc_md9391}
Type\+: {\ttfamily string} {\ttfamily number} {\ttfamily Stream} {\ttfamily undefined} {\ttfamily null}~\newline
 Default\+: {\ttfamily pipe}

Same options as \href{https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html\#child_process_options_stdio}{\texttt{ {\ttfamily stdio}}}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9392}{}\doxysubsubsection{stdout}\label{md_sfcode_node_modules_execa_readme_autotoc_md9392}
Type\+: {\ttfamily string} {\ttfamily number} {\ttfamily Stream} {\ttfamily undefined} {\ttfamily null}~\newline
 Default\+: {\ttfamily pipe}

Same options as \href{https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html\#child_process_options_stdio}{\texttt{ {\ttfamily stdio}}}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9393}{}\doxysubsubsection{stderr}\label{md_sfcode_node_modules_execa_readme_autotoc_md9393}
Type\+: {\ttfamily string} {\ttfamily number} {\ttfamily Stream} {\ttfamily undefined} {\ttfamily null}~\newline
 Default\+: {\ttfamily pipe}

Same options as \href{https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html\#child_process_options_stdio}{\texttt{ {\ttfamily stdio}}}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9394}{}\doxysubsubsection{windows\+Verbatim\+Arguments}\label{md_sfcode_node_modules_execa_readme_autotoc_md9394}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily false}

If {\ttfamily true}, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to {\ttfamily true} automatically when the {\ttfamily shell} option is {\ttfamily true}.\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9395}{}\doxysection{Tips}\label{md_sfcode_node_modules_execa_readme_autotoc_md9395}
\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9396}{}\doxysubsection{Save and pipe output from a child process}\label{md_sfcode_node_modules_execa_readme_autotoc_md9396}
Let\textquotesingle{}s say you want to show the output of a child process in real-\/time while also saving it to a variable.


\begin{DoxyCode}{0}
\DoxyCodeLine{const execa = require('execa');}
\DoxyCodeLine{const getStream = require('get-\/stream');}
\DoxyCodeLine{}
\DoxyCodeLine{const stream = execa('echo', ['foo']).stdout;}
\DoxyCodeLine{}
\DoxyCodeLine{stream.pipe(process.stdout);}
\DoxyCodeLine{}
\DoxyCodeLine{getStream(stream).then(value => \{}
\DoxyCodeLine{    console.log('child output:', value);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_execa_readme_autotoc_md9397}{}\doxysection{License}\label{md_sfcode_node_modules_execa_readme_autotoc_md9397}
MIT © \href{https://sindresorhus.com}{\texttt{ Sindre Sorhus}} 