deepwiki-lab
Relevant source files
  • test/arguments/command-resolution.js
  • test/arguments/cwd.js
  • test/arguments/encoding-option.js
  • test/arguments/env.js
  • test/arguments/escape-no-icu.js
  • test/arguments/escape.js
  • test/arguments/fd-options.js
  • test/arguments/local.js
  • index.js
  • test-d/arguments/env.test-d.ts
  • docs/typescript.md
  • test/helpers/run.js
  • readme.md
  • lib/methods/promise.js
  • test/methods/main-async.js
  • docs/scripts.md
  • docs/bash.md
  • test/fixtures/ipc-any.js

Execa Documentation

Examples

Execa provides a simple and feature-rich way to execute commands in your Node.js project. Here are some examples of how to use Execa:

Simple Syntax

import {execa} from 'execa';

const {stdout} = await execa`npm run build`;
console.log(stdout);

Script Interface

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

await execa({preferLocal: true})`eslint`;

Pipe Multiple Subprocesses

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

console.log(stdout);
console.log(pipedFrom[0].stdout);
console.log(pipedFrom[0].pipedFrom[0].stdout);

Input/Output

import {execa} from 'execa';

const {stdout} = await execa`echo Hello World`;
console.log(stdout);

const {stderr} = await execa`echo Hello World 2>&1`;
console.log(stderr);

Advanced Usage

import {execa} from 'execa';

const {stdout} = await execa`sleep 1`;
console.log(stdout);

const {stderr} = await execa`sleep 1 2>&1`;
console.log(stderr);

Interleaved Output

import {execa} from 'execa';

const {stdout, stderr} = await execa`echo Hello World && echo Error`;
console.log(stdout);
console.log(stderr);

Transform/Filter Output

import {execa} from 'execa';

const {stdout} = await execa`echo Hello World | tr ' ' '-'`;
console.log(stdout);

Exchange Messages

import {execa} from 'execa';

const {stdout} = await execa`echo Hello World | execa --message=Hello`;
console.log(stdout);

Sources:

index.js test-d/arguments/env.test-d.ts docs/typescript.md test/helpers/run.js readme.md lib/methods/promise.js test/methods/main-async.js docs/scripts.md docs/bash.md test/fixtures/ipc-any.js