execa child processes with errors, timeouts and input: working versions (tested 2026-10-15)
Run child processes with execa 10: capture stdout and stderr, pass stdin input, split lines, handle non-zero exits with ExecaError or reject: false, and kill slow commands with a timeout.
passed6 passed · 0 failed2026-10-15 17:10 UTC- Status
- passed
- Last passed
- 2026-10-15 17:10 UTC
- Tests
- 6 / 6
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
execa@10.1.1@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38775053802
The code
Exactly the files that passed. Your agent can pull them with get_recipe("execa-child-processes") (install), or see them on GitHub.
package.json
{
"name": "execa-child-processes",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"execa": "10.1.1"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/proc.ts
import { execa, ExecaError } from "execa";
const node = process.execPath;
/** Run a small Node script as a subprocess (stands in for any CLI tool). */
export function runScript(code: string, opts: { input?: string; timeout?: number; env?: Record<string, string> } = {}) {
return execa(node, ["-e", code], { input: opts.input, timeout: opts.timeout, env: opts.env });
}
export async function upperCase(text: string): Promise<string> {
const { stdout } = await runScript("process.stdin.on('data', d => process.stdout.write(String(d).toUpperCase()))", { input: text });
return stdout;
}
export async function listLines(count: number): Promise<string[]> {
const { stdout } = await execa(node, ["-e", `for (let i = 1; i <= ${count}; i++) console.log("line " + i)`], { lines: true });
return stdout;
}
export async function exitInfo(code: string) {
const r = await execa(node, ["-e", code], { reject: false });
return { exitCode: r.exitCode, failed: r.failed, stderr: r.stderr };
}
export async function tryRun(code: string, timeout?: number) {
try {
await runScript(code, { timeout });
return { ok: true as const };
} catch (e) {
if (e instanceof ExecaError) return { ok: false as const, exitCode: e.exitCode, timedOut: e.timedOut, stderr: e.stderr, short: e.shortMessage };
throw e;
}
}
test/proc.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { runScript, upperCase, listLines, exitInfo, tryRun } from "../src/proc.ts";
test("captures stdout, stderr and env", async () => {
const r = await runScript("console.log(process.env.GREETING); console.error('warn')", { env: { GREETING: "hello" } });
assert.equal(r.stdout, "hello");
assert.equal(r.stderr, "warn");
assert.equal(r.exitCode, 0);
});
test("pipes input to stdin", async () => {
assert.equal(await upperCase("abc"), "ABC");
});
test("splits output into lines", async () => {
assert.deepEqual(await listLines(3), ["line 1", "line 2", "line 3"]);
});
test("reject: false returns the exit code instead of throwing", async () => {
const r = await exitInfo("console.error('boom'); process.exit(3)");
assert.deepEqual(r, { exitCode: 3, failed: true, stderr: "boom" });
});
test("non-zero exits throw ExecaError with details", async () => {
const r = await tryRun("process.exit(2)");
assert.equal(r.ok, false);
if (!r.ok) {
assert.equal(r.exitCode, 2);
assert.equal(r.timedOut, false);
}
});
test("timeouts kill hung commands", async () => {
const r = await tryRun("setTimeout(() => {}, 10000)", 200);
assert.equal(r.ok, false);
if (!r.ok) assert.equal(r.timedOut, true);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.