execa
Process execution helper for Node.js that returns clean stdout/stderr as promises and avoids shell quoting pitfalls.
Official Documentation ↗v9. Check the official docs for the latest API surface.Installation
npm install execapnpm add execaMethods
No methods match this filter.
Process execution helper for Node.js that returns clean stdout/stderr as promises and avoids shell quoting pitfalls.
Overview
execa wraps child_process with a promise API that fixes the sharp edges: output has no stray trailing newline, errors carry exit codes and stderr, and Windows quirks such as .cmd resolution and shell quoting are handled.
It stays close to the platform while adding practical conveniences, including stdin/stdout piping to files or streams, cancellation through AbortSignal, timeout kills, and synchronous variants for CLI bootstrap code. The $ tagged template in v9 gives an exec literal with interpolated arguments that stay correctly quoted.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
execa— Runs a binary with an argument array and resolves with stdout, stderr, exitCode, and the command line.execaCommand— Parses a full command string with spaces and quotes, then runs it without an intermediate shell process.execaSync— Synchronous variant that blocks the event loop until the process exits; suited to startup scripts only.reject / shell options— Controls failure semantics and environment: reject false resolves non-zero exits, shell runs through /bin/sh, timeout kills long runs.
Usage Notes
- Prefer the argument array form over execaCommand whenever any value comes from user input; it removes an entire injection class.
- Use reject: false when a non-zero exit is a normal outcome, such as probing whether a command exists, and branch on exitCode.
- Pass env objects explicitly rather than mutating process.env so child processes receive a reproducible environment.
Common Pitfalls
- Enabling shell with interpolated strings reopens injection risk: a value like “; rm -rf” becomes shell syntax.
- execaSync blocks the event loop; calling it inside request handling stalls every concurrent request.
- On Windows, batch files and PowerShell entry points need the right file extension or shell mode to resolve.
FAQ
- Why does execa avoid the shell by default?
- Passing an argument array means every value reaches the child process as one argv entry, so filenames with spaces or embedded quotes cannot be reinterpreted as shell syntax. Enable shell only for commands you fully control.
- How do I capture large output safely?
- stdout and stderr are fully buffered into strings or Buffers, so extremely chatty processes consume memory. For big output, switch to streaming mode where result.stdout arrives chunk by chunk, or write to a file with an output file option.
- What does the returned Result contain?
- Each resolved result carries stdout and stderr cleaned of trailing newlines, exitCode, failed, command showing the invocation, all, and timedOut. On rejection the error object exposes the same fields plus the original message.
Report a problem with this API docOpens a pre-filled issue on GitHub.