Skip to content
Myr

readme.md

This file

Path

readme.md

320 lines · 8.9 KiB · Markdown

Last changed by

Add a sentence about job search (#1027)ehmickybefore adoption5182195

History of this file

Take it

The walk stopped after 200 commits: lines older than a61a028 point at it.

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 1–33why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="media/logo_dark.svg">3 <img alt="execa logo" src="media/logo.svg" width="400">4</picture>5<br>67[![Coverage Status](https://codecov.io/gh/sindresorhus/execa/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/execa)89> Process execution for humans1011<br>1213---1415<div align="center">16 <p>17 <p>18 <sup>19 <a href="https://github.com/sponsors/sindresorhus">Sindre's open source work is supported by the community</a>20 </sup>21 </p>22 <sup>Special thanks to:</sup>23 <br>24 <br>25 <a href="https://transloadit.com?utm_source=sindresorhus&utm_medium=referral&utm_campaign=sponsorship&utm_content=execa">26 <picture>27 <source width="360" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo-dark.svg">28 <source width="360" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo.svg">29 <img width="360" src="https://sindresorhus.com/assets/thanks/transloadit-logo.svg" alt="Transloadit logo">30 </picture>31 </a>32 <br>33 <br>

Meta tweaksSindre Sorhusbefore adoption1915060

lines 34–38why, one step back
34 <a href="https://coderabbit.ai?utm_source=sindre&utm_medium=execa">
35 <img width="300" src="https://sindresorhus.com/assets/thanks/coderabbit-logo.png" alt="CodeRabbit logo">36 </a>37 <br>38 <br>

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 39–45why, one step back
39 </p>
40</div>4142---4344<br>45

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 46why, one step back
46Execa runs commands in your script, application or library. Unlike shells, it is [optimized](docs/bash.md) for programmatic usage. Built on top of the [`child_process`](https://nodejs.org/api/child_process.html) core module.

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 47why, one step back
47

Add a sentence about job search (#1027)ehmickybefore adoption5182195

lines 48–53why, one step back
48---
4950One of the maintainers [@ehmicky](https://github.com/ehmicky) is looking for a remote full-time position. Specialized in Node.js back-ends and CLIs, he led Netlify [Build](https://www.netlify.com/platform/core/build/), [Plugins](https://www.netlify.com/integrations/) and Configuration for 2.5 years. Feel free to contact him on [his website](https://www.mickael-hebert.com) or on [LinkedIn](https://www.linkedin.com/in/mickaelhebert/)!5152---53

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 54why, one step back
54## Features

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 55why, one step back
55

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 56–70why, one step back
56- [Simple syntax](#simple-syntax): promises and [template strings](docs/execution.md#template-string-syntax), like [`zx`](docs/bash.md).
57- [Script](#script) interface.58- [No escaping](docs/escaping.md) nor quoting needed. No risk of shell injection.59- Execute [locally installed binaries](#local-binaries) without `npx`.60- Improved [Windows support](docs/windows.md): [shebangs](docs/windows.md#shebang), [`PATHEXT`](https://ss64.com/nt/path.html#pathext), [and more](https://github.com/moxystudio/node-cross-spawn?tab=readme-ov-file#why).61- [Detailed errors](#detailed-error) and [verbose mode](#verbose-mode), for [debugging](docs/debugging.md).62- [Pipe multiple subprocesses](#pipe-multiple-subprocesses) better than in shells: retrieve [intermediate results](docs/pipe.md#result), use multiple [sources](docs/pipe.md#multiple-sources-1-destination)/[destinations](docs/pipe.md#1-source-multiple-destinations), [unpipe](docs/pipe.md#unpipe).63- [Split](#split-into-text-lines) the output into text lines, or [iterate](#iterate-over-text-lines) progressively over them.64- Strip [unnecessary newlines](docs/lines.md#newlines).65- Get [interleaved output](#interleaved-output) from `stdout` and `stderr` similar to what is printed on the terminal.66- Retrieve the output [programmatically and print it](#programmatic--terminal-output) on the console at the same time.67- [Transform or filter](#transformfilter-output) the input and output with [simple functions](docs/transform.md).68- Redirect the [input](docs/input.md) and [output](docs/output.md) from/to [files](#files), [strings](#simple-input), [`Uint8Array`s](docs/binary.md#binary-input), [iterables](docs/streams.md#iterables-as-input) or [objects](docs/transform.md#object-mode).69- Pass [Node.js streams](docs/streams.md#nodejs-streams) or [web streams](#web-streams) to subprocesses, or [convert](#convert-to-duplex-stream) subprocesses to [a stream](docs/streams.md#converting-a-subprocess-to-a-stream).70- Ensure subprocesses exit even when they [intercept termination signals](docs/termination.md#forceful-termination), or when the current process [ends abruptly](docs/termination.md#current-process-exit).

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 71–77why, one step back
71
72## Install7374```sh75npm install execa76```77

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 78–103why, one step back
78## Documentation
7980Execution:81- ▶️ [Basic execution](docs/execution.md)82- 💬 [Escaping/quoting](docs/escaping.md)83- 💻 [Shell](docs/shell.md)84- 📜 [Scripts](docs/scripts.md)85- 🐢 [Node.js files](docs/node.md)86- 🌐 [Environment](docs/environment.md)87- ❌ [Errors](docs/errors.md)88- 🏁 [Termination](docs/termination.md)8990Input/output:91- 🎹 [Input](docs/input.md)92- 📢 [Output](docs/output.md)93- 📃 [Text lines](docs/lines.md)94- 🤖 [Binary data](docs/binary.md)95- 🧙 [Transforms](docs/transform.md)9697Advanced usage:98- 🔀 [Piping multiple subprocesses](docs/pipe.md)99- ⏳️ [Streams](docs/streams.md)100- 📞 [Inter-process communication](docs/ipc.md)101- 🐛 [Debugging](docs/debugging.md)102- 📎 [Windows](docs/windows.md)103- 🔍 [Difference with Bash and zx](docs/bash.md)

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

line 104why, one step back
104- 🤓 [TypeScript](docs/typescript.md)

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 105why, one step back
105- 📔 [API reference](docs/api.md)

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 106why, one step back
106

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 107why, one step back
107## Examples

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 108why, one step back
108

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 109why, one step back
109### Execution

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 110why, one step back
110

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 111–112why, one step back
111#### Simple syntax
112

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 113–115why, one step back
113```js
114import {execa} from 'execa';115

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 116–117why, one step back
116const {stdout} = await execa`npm run build`;
117// Print command's output

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 118–120why, one step back
118console.log(stdout);
119```120

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 121why, one step back
121#### Script

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 122why, one step back
122

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 123why, one step back
123```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 124why, one step back
124import {$} from 'execa';

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 125why, one step back
125

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 126–127why, one step back
126const {stdout: name} = await $`cat package.json`.pipe`grep name`;
127console.log(name);

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 128why, one step back
128

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 129–130why, one step back
129const branch = await $`git branch --show-current`;
130await $`dep deploy --branch=${branch}`;

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 131why, one step back
131

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 132–136why, one step back
132await Promise.all([
133 $`sleep 1`,134 $`sleep 2`,135 $`sleep 3`,136]);

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 137why, one step back
137

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 138–140why, one step back
138const directoryName = 'foo bar';
139await $`mkdir /tmp/${directoryName}`;140```

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 141why, one step back
141

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 142why, one step back
142#### Local binaries

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 143why, one step back
143

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 144–145why, one step back
144```sh
145$ npm install -D eslint

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 146–148why, one step back
146```
147148```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 149why, one step back
149await execa({preferLocal: true})`eslint`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 150–151why, one step back
150```
151

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 152why, one step back
152#### Pipe multiple subprocesses

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 153–154why, one step back
153
154```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 155–157why, one step back
155const {stdout, pipedFrom} = await execa`npm run build`
156 .pipe`sort`157 .pipe`head -n 2`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 158why, one step back
158

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 159–164why, one step back
159// Output of `npm run build | sort | head -n 2`
160console.log(stdout);161// Output of `npm run build | sort`162console.log(pipedFrom[0].stdout);163// Output of `npm run build`164console.log(pipedFrom[0].pipedFrom[0].stdout);

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 165–166why, one step back
165```
166

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 167why, one step back
167### Input/output

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 168why, one step back
168

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 169why, one step back
169#### Interleaved output

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 170why, one step back
170

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

line 171why, one step back
171```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 172–174why, one step back
172const {all} = await execa({all: true})`npm run build`;
173// stdout + stderr, interleaved174console.log(all);

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

lines 175–176why, one step back
175```
176

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 177why, one step back
177#### Programmatic + terminal output

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 178why, one step back
178

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 179–182why, one step back
179```js
180const {stdout} = await execa({stdout: ['pipe', 'inherit']})`npm run build`;181// stdout is also printed to the terminal182console.log(stdout);

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 183–184why, one step back
183```
184

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 185why, one step back
185#### Files

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 186–187why, one step back
186
187```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 188–189why, one step back
188// Similar to: npm run build > output.txt
189await execa({stdout: {file: './output.txt'}})`npm run build`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 190–191why, one step back
190```
191

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 192why, one step back
192#### Simple input

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 193–194why, one step back
193
194```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 195why, one step back
195const {stdout} = await execa({input: getInputString()})`sort`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 196–198why, one step back
196console.log(stdout);
197```198

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 199why, one step back
199#### Split into text lines

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 200–201why, one step back
200
201```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 202–205why, one step back
202const {stdout} = await execa({lines: true})`npm run build`;
203// Print first 10 lines204console.log(stdout.slice(0, 10).join('\n'));205```

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 206why, one step back
206

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 207–216why, one step back
207### Streaming
208209#### Iterate over text lines210211```js212for await (const line of execa`npm run build`) {213 if (line.includes('WARN')) {214 console.warn(line);215 }216}

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 217–218why, one step back
217```
218

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 219why, one step back
219#### Transform/filter output

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 220–221why, one step back
220
221```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 222why, one step back
222let count = 0;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 223why, one step back
223

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 224–231why, one step back
224// Filter out secret lines, then prepend the line number
225const transform = function * (line) {226 if (!line.includes('secret')) {227 yield `[${count++}] ${line}`;228 }229};230231await execa({stdout: transform})`npm run build`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 232–233why, one step back
232```
233

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 234why, one step back
234#### Web streams

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

lines 235–236why, one step back
235
236```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 237–238why, one step back
237const response = await fetch('https://example.com');
238await execa({stdin: response.body})`sort`;

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

lines 239–240why, one step back
239```
240

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 241why, one step back
241#### Convert to Duplex stream

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 242–244why, one step back
242
243```js244import {execa} from 'execa';

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 245–246why, one step back
245import {pipeline} from 'node:stream/promises';
246import {createReadStream, createWriteStream} from 'node:fs';

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 247why, one step back
247

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 248–252why, one step back
248await pipeline(
249 createReadStream('./input.txt'),250 execa`node ./transform.js`.duplex(),251 createWriteStream('./output.txt'),252);

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 253–254why, one step back
253```
254

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 255why, one step back
255### Debugging

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 256why, one step back
256

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 257–258why, one step back
257#### Detailed error
258

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 259why, one step back
259```js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 260why, one step back
260import {execa, ExecaError} from 'execa';

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 261–262why, one step back
261
262try {

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 263why, one step back
263 await execa`unknown command`;

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 264why, one step back
264} catch (error) {

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 265–267why, one step back
265 if (error instanceof ExecaError) {
266 console.log(error);267 }

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

line 268why, one step back
268 /*

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 269–272why, one step back
269 ExecaError: Command failed with ENOENT: unknown command
270 spawn unknown ENOENT271 at ...272 at ... {

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 273why, one step back
273 shortMessage: 'Command failed with ENOENT: unknown command\nspawn unknown ENOENT',

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 274–276why, one step back
274 originalMessage: 'spawn unknown ENOENT',
275 command: 'unknown command',276 escapedCommand: 'unknown command',

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

lines 277–278why, one step back
277 cwd: '/path/to/cwd',
278 durationMs: 28.217566,

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 279–281why, one step back
279 failed: true,
280 timedOut: false,281 isCanceled: false,

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 282why, one step back
282 isTerminated: false,

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 283why, one step back
283 isMaxBuffer: false,

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 284why, one step back
284 code: 'ENOENT',

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

lines 285–288why, one step back
285 stdout: '',
286 stderr: '',287 stdio: [undefined, '', ''],288 pipedFrom: []

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 289–297why, one step back
289 [cause]: Error: spawn unknown ENOENT
290 at ...291 at ... {292 errno: -2,293 code: 'ENOENT',294 syscall: 'spawn unknown',295 path: 'unknown',296 spawnargs: [ 'command' ]297 }

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 298–301why, one step back
298 }
299 */300}301```

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 302–310why, one step back
302
303#### Verbose mode304305```js306await execa`npm run build`;307await execa`npm run test`;308```309310<img alt="execa verbose output" src="media/verbose.png" width="603">

Fix `timeout` option (#727)ehmickybefore adoptiona61a028

lines 311–320why, one step back
311
312## Related313314- [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for Execa315- [nvexeca](https://github.com/ehmicky/nvexeca) - Run Execa using any Node.js version316317## Maintainers318319- [Sindre Sorhus](https://github.com/sindresorhus)320- [@ehmicky](https://github.com/ehmicky)