All Tips

Tool of the Trade for CLI Apps - `execa` 🖥

This is a series of libraries and utilities for building Command-line-interfaces aka CLI using Node.js and ecosystem around it. Similar utilities/helpers may be available for other languages but, these series is focused on JavaScript and its friends.

You can check all articles under CLI Build Tool tag.

execa

If you are building a CLI and spawn a process to execute some external task/action, you can use child_process to create Synchronous Process or Asynchronous Process. This works pretty well in most cases but execa offers more.

As advertised, this package improves child_process methods with:

  • Promise interface.
  • Strips the final newline from the output so you don’t have to do stdout.trim().
  • Supports shebang binaries cross-platform.
  • Improved Windows support.
  • Higher max buffer. 100 MB instead of 200 KB.
  • Executes locally installed binaries by name.
  • Cleans up spawned processes when the parent process dies.
  • Get interleaved output from stdout and stderr similar to what is printed on the terminal. (Async only)
  • Can specify file and arguments as a single string without a shell
  • More descriptive errors.

It provides APIs in both variants , async (Promise) and sync. execa has a very easy to follow, simple to use API. Here is a code example:

const execa = require('execa');

(async () => {
	const { stdout } = await execa('echo', ['execa: A better child process management']);
	console.log(stdout);
	//=> 'execa: A better child process management'
})();

You can check out official repo for all usage details.

Github Repo: execa


Published Jul 2, 2019

Web/Product Engineer.