Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
E

execa

> 编程语言
Open source

Process execution for humans

7.6K stars0 likes2 views
WebsiteGitHub

About

Process execution for humans


> Process execution for humans

		<b>Fast remote container builds and GitHub Actions runners.</b>
	</a>
	<br>
	<br>
	<br>
</p>


Execa runs commands in your script, application or library. Unlike shells, it is optimized for programmatic usage. Built on top of the child_process core module.

Features

  • Simple syntax: promises and template strings, like zx.
  • Script interface.
  • No escaping nor quoting needed. No risk of shell injection.
  • Execute locally installed binaries without npx.
  • Improved Windows support: shebangs, PATHEXT, graceful termination, and more.
  • Detailed errors, verbose mode and custom logging, for debugging.
  • Pipe multiple subprocesses better than in shells: retrieve intermediate results, use multiple sources/destinations, unpipe.
  • Split the output into text lines, or iterate progressively over them.
  • Strip unnecessary newlines.
  • Pass any input to the subprocess: files, strings, Uint8Arrays, iterables, objects and almost any other type.
  • Return almost any type from the subprocess, or redirect it to files.
  • Get interleaved output from stdout and stderr similar to what is printed on the terminal.
  • Retrieve the output programmatically and print it on the console at the same time.
  • Transform or filter the input and output with simple functions.
  • Pass Node.js streams or web streams to subprocesses, or convert subprocesses to a stream.
  • Exchange messages with the subprocess.
  • Ensure subprocesses exit even when they intercept termination signals, or when the current process ends abruptly.

Install

bash
npm install execa

Documentation

Execution:

  • ▶️ Basic execution
  • Escaping/quoting
  • Shell
  • Scripts
  • Node.js files
  • Environment
  • ❌ Errors
  • Termination

Input/output:

  • Input
  • Output
  • Text lines
  • Binary data
  • Transforms

Advanced usage:

  • Piping multiple subprocesses
  • ⏳️ Streams
  • Inter-process communication
  • Debugging
  • Windows
  • Difference with Bash and zx
  • Small packages
  • TypeScript
  • API reference

Examples

Execution

Simple syntax

javascript
import {execa} from 'execa';

const {stdout} = await execa`npm run build`;
// Print command's output
console.log(stdout);

Script

javascript
import {$} from 'execa';

const {stdout: name} = await $`cat package.json`.pipe`grep name`;
console.log(name);

const branch = await $`git branch --show-current`;
await $`dep deploy --branch=${branch}`;

await Promise.all([
	$`sleep 1`,
	$`sleep 2`,
	$`sleep 3`,
]);

const directoryName = 'foo bar';
await $`mkdir /tmp/${directoryName}`;

Local binaries

bash
$ npm install -D eslint
javascript
await execa({preferLocal: true})`eslint`;

Pipe multiple subprocesses

javascript
const {stdout, pipedFrom} = await execa`npm run build`
	.pipe`sort`
	.pipe`head -n 2`;

// Output of `npm run build | sort | head -n 2`
console.log(stdout);
// Output of `npm run build | sort`
console.log(pipedFrom[0].stdout);
// Output of `npm run build`
console.log(pipedFrom[0].pipedFrom[0].stdout);

Input/output

Interleaved output

javascript
const {all} = await execa({all: true})`npm run build`;
// stdout + stderr, interleaved
console.log(all);

Programmatic + terminal output

javascript
const {stdout} = await execa({stdout: ['pipe', 'inherit']})`npm run build`;
// stdout is also printed to the terminal
console.log(stdout);

Simple input

javascript
const getInputString = () => { /* ... */ };
const {stdout} = await execa({input: getInputString()})`sort`;
console.log(stdout);

File input

javascript
// Similar to: npm run build < input.txt
await execa({stdin: {file: 'input.txt'}})`npm run build`;

File output

javascript
// Similar to: npm run build > output.txt
await execa({stdout: {file: 'output.txt'}})`npm run build`;

Split into text lines

javascript
const {stdout} = await execa({lines: true})`npm run build`;
// Print first 10 lines
console.log(stdout.slice(0, 10).join('\n'));

Streaming

Iterate over text lines

javascript
for await (const line of execa`npm run build`) {
	if (line.includes('WARN')) {
		console.warn(line);
	}
}

Transform/filter output

javascript
let count = 0;

// Filter out secret lines, then prepend the line number
const transform = function * (line) {
	if (!line.includes('secret')) {
		yield `[${count++}] ${line}`;
	}
};

await execa({stdout: transform})`npm run build`;

Web streams

javascript
const response = await fetch('https://example.com');
await execa({stdin: response.body})`sort`;

Convert to Duplex stream

javascript
import {execa} from 'execa';
import {pipeline} from 'node:stream/promises';
import {createReadStream, createWriteStream} from 'node:fs';

await pipeline(
	createReadStream('./input.txt'),
	execa`node ./transform.js`.duplex(),
	createWriteStream('./output.txt'),
);

IPC

Exchange messages

javascript
// parent.js
import {execaNode} from 'execa';

const subprocess = execaNode`child.js`;
await subprocess.sendMessage('Hello from parent');
const message = await subprocess.getOneMessage();
console.log(message); // 'Hello from child'
javascript
// child.js
import {getOneMessage, sendMessage} from 'execa';

const message = await getOneMessage(); // 'Hello from parent'
const newMessage = message.replace('parent', 'child'); // 'Hello from child'
await sendMessage(newMessage);

Any input type

javascript
// main.js
import {execaNode} from 'execa';

const ipcInput = [
	{task: 'lint', ignore: /test\.js/},
	{task: 'copy', files: new Set(['main.js', 'index.js']),
}];
await execaNode({ipcInput})`build.js`;
javascript
// build.js
import {getOneMessage} from 'execa';

const ipcInput = await getOneMessage();

Any output type

javascript
// main.js
import {execaNode} from 'execa';

const {ipcOutput} = await execaNode`build.js`;
console.log(ipcOutput[0]); // {kind: 'start', timestamp: date}
console.log(ipcOutput[1]); // {kind: 'stop', timestamp: date}
javascript
// build.js
import {sendMessage} from 'execa';

const runBuild = () => { /* ... */ };

await sendMessage({kind: 'start', timestamp: new Date()});
await runBuild();
await sendMessage({kind: 'stop', timestamp: new Date()});

Graceful termination

javascript
// main.js
import {execaNode} from 'execa';

const controller = new AbortController();
setTimeout(() => {
	controller.abort();
}, 5000);

await execaNode({
	cancelSignal: controller.signal,
	gracefulCancel: true,
})`build.js`;
javascript
// build.js
import {getCancelSignal} from 'execa';

const cancelSignal = await getCancelSignal();
const url = 'https://example.com/build/info';
const response = await fetch(url, {signal: cancelSignal});

Debugging

Detailed error

…

Verbose mode

javascript
await execa`npm run build`;
await execa`npm run test`;

Custom logging

javascript
import {execa as execa_} from 'execa';
import {createLogger, transports} from 'winston';

// Log to a file using Winston
const transport = new transports.File({filename: 'logs.txt'});
const logger = createLogger({transports: [transport]});
const LOG_LEVELS = {
	command: 'info',
	output: 'verbose',
	ipc: 'verbose',
	error: 'error',
	duration: 'info',
};

const execa = execa_({
	verbose(verboseLine, {message, ...verboseObject}) {
		const level = LOG_LEVELS[verboseObject.type];
		logger[level](message, verboseObject);
	},
});

await execa`npm run build`;
await execa`npm run test`;

Related

  • nano-spawn - Like Execa but smaller
  • gulp-execa - Gulp plugin for Execa
  • nvexeca - Run Execa using any Node.js version

Maintainers

  • Sindre Sorhus
  • @ehmicky

Issues· 3 open

View all issuesOpen on GitHub
  • #1262

    `execa()` throws `subprocess.stdio is not iterable` and the process crashes when spawning fails with `EMFILE`

    Updated Oct 1, 2026
  • #1261

    Execa 10 - Windows: arguments are double caret-escaped for every batch file, not just cmd-shims

    Updated Sep 27, 2026
  • #1001

    Document and test async iterator helpers

    Updated Aug 11, 2026

> Tags

JavaScriptbinarychild-processexecexecute

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言