node:child_process
Node-compatible synchronous and async child process execution without a shell.
Available in vjs (vjs-lite omits async execFile and exposes only
synchronous spawnSync and execFileSync). Enabled by default unless
--no-node-compat is passed.
For safety, executables are invoked directly without spawning a shell;
passing shell: true fails with ERR_UNSUPPORTED.
import ... from "node:child_process"
node-compat (enabled by default; disable with --no-node-compat)execFile is V8-only (vjs). spawnSync and execFileSync run on both vjs and vjs-lite.Example
Section titled “Example”import { execFile, spawnSync } from "node:child_process";
const syncRes = spawnSync("echo", ["hello"], { encoding: "utf8" });console.log(syncRes.status, syncRes.stdout.trim());
const { stdout } = await execFile("echo", ["async"], { encoding: "utf8" });console.log(stdout.trim());Functions
Section titled “Functions”spawnSync
Section titled “spawnSync”export function spawnSync(command: string, args?: readonly string[], options?: ChildProcessOptions & { encoding?: "buffer" | null }): SpawnSyncReturns<Uint8Array>;
export function spawnSync(command: string, args: readonly string[] | undefined, options: ChildProcessOptions & { encoding: TextEncoding }): SpawnSyncReturns<string>;
export function spawnSync(command: string, options?: ChildProcessOptions): SpawnSyncReturns<Uint8Array | string>;
Synchronously spawn command returning raw Uint8Array stdout/stderr.
Executes command directly without a shell (max 4096 args). Non-zero exit
codes do not throw; inspect status and error on the returned object.
spawnSync(command, args?, options?)| Name | Type | Description |
|---|---|---|
command | string | Executable name or path. |
args optional | readonly string[] | Argument list passed to command (max 4096 entries). |
options optional | ChildProcessOptions & { encoding?: "buffer" | null } | Execution options with binary output (encoding: "buffer" | null or omitted). |
SpawnSyncReturns<Uint8Array> — Captured exit status, signal, stdout, stderr, optional error, and timedOut flag.spawnSync(command, args, options)Synchronously spawn command returning string-decoded stdout/stderr.
| Name | Type | Description |
|---|---|---|
command | string | Executable name or path. |
args | readonly string[] | undefined | Argument list passed to command (max 4096 entries), or undefined. |
options | ChildProcessOptions & { encoding: TextEncoding } | Execution options specifying a TextEncoding for stdout and stderr. |
SpawnSyncReturns<string> — Captured exit status, signal, string stdout, string stderr, optional error, and timedOut flag.spawnSync(command, options?)Synchronously spawn command with options.
| Name | Type | Description |
|---|---|---|
command | string | Executable name or path. |
options optional | ChildProcessOptions | Execution options without an explicit args array. |
SpawnSyncReturns<Uint8Array | string> — Captured exit status, signal, stdout, stderr, optional error, and timedOut flag.TypeError— when arguments or options have invalid types, orencodingis unknown (ERR_UNKNOWN_ENCODING).RangeError— whenargs/envlimits are exceeded ortimeout/maxBufferis invalid; whenenvlimits are exceeded ortimeout/maxBufferis invalid.Error— withcode = "ERR_UNSUPPORTED"ifshellis enabled inoptions.
import { Buffer } from "node:buffer";import { spawnSync } from "node:child_process";
const res = spawnSync("echo", ["raw-bytes"]);console.log(Buffer.from(res.stdout).toString("utf8").trim());execFileSync
Section titled “execFileSync”export function execFileSync(file: string, args?: readonly string[], options?: ChildProcessOptions & { encoding?: "buffer" | null }): Uint8Array;
export function execFileSync(file: string, args: readonly string[] | undefined, options: ChildProcessOptions & { encoding: TextEncoding }): string;
export function execFileSync(file: string, options?: ChildProcessOptions): Uint8Array | string;
Synchronously execute file returning raw Uint8Array stdout.
Unlike spawnSync, throws an Error (with status, signal, stdout,
stderr, timedOut, and code) if the process fails to spawn, times out,
or exits with a non-zero status.
execFileSync(file, args?, options?)| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
args optional | readonly string[] | Argument list passed to file (max 4096 entries). |
options optional | ChildProcessOptions & { encoding?: "buffer" | null } | Execution options with binary output. |
Uint8Array — Captured stdout bytes.execFileSync(file, args, options)Synchronously execute file returning string-decoded stdout.
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
args | readonly string[] | undefined | Argument list passed to file (max 4096 entries), or undefined. |
options | ChildProcessOptions & { encoding: TextEncoding } | Execution options specifying a TextEncoding for stdout. |
string — Decoded stdout string.execFileSync(file, options?)Synchronously execute file with options.
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
options optional | ChildProcessOptions | Execution options without an explicit args array. |
TypeError— when arguments or options have invalid types, orencodingis unknown (ERR_UNKNOWN_ENCODING).RangeError— whenargs/envlimits are exceeded ortimeout/maxBufferis invalid; whenenvlimits are exceeded ortimeout/maxBufferis invalid.Error— on non-zero exit, signal termination, timeout (ETIMEDOUT), spawn failure (ENOENT,EACCES,EIO), orERR_UNSUPPORTEDifshellis enabled.
import { execFileSync } from "node:child_process";
const out = execFileSync("echo", ["hello"], { encoding: "utf8" });console.log(out.trim());execFile
Section titled “execFile”export function execFile(file: string, options?: ChildProcessOptions): Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }>;
export function execFile(file: string, args?: readonly string[], options?: ChildProcessOptions): Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }>;
export function execFile(
file: string,
callback: (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void,
): void;
export function execFile(
file: string,
args: readonly string[],
callback: (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void,
): void;
export function execFile(
file: string,
args: readonly string[],
options: ChildProcessOptions,
callback: (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void,
): void;
Executes file asynchronously without spawning a shell.
Resolves to { stdout, stderr } when called without a callback, or invokes
callback(err, stdout, stderr) when a trailing callback is provided.
Available in vjs only (vjs-lite provides synchronous spawnSync and
execFileSync only).
execFile(file, options?)| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
options optional | ChildProcessOptions | Execution options. |
Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }> — Promise resolving to { stdout, stderr }, or rejecting on spawn failure, timeout, or non-zero exit.execFile(file, args?, options?)Execute file asynchronously with args and optional options, returning a Promise.
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
args optional | readonly string[] | Argument list passed to file (max 4096 entries). |
options optional | ChildProcessOptions | Execution options. |
Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }> — Promise resolving to { stdout, stderr }, or rejecting on spawn failure, timeout, or non-zero exit.execFile(file, callback)Execute file asynchronously and invoke callback(error, stdout, stderr).
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
callback | (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void | Node-style callback invoked with (error, stdout, stderr) upon completion. |
voidexecFile(file, args, callback)Execute file asynchronously with args and invoke callback(error, stdout, stderr).
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
args | readonly string[] | Argument list passed to file (max 4096 entries). |
callback | (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void | Node-style callback invoked with (error, stdout, stderr) upon completion. |
voidexecFile(file, args, options, callback)Execute file asynchronously with args and options, invoking callback(error, stdout, stderr).
| Name | Type | Description |
|---|---|---|
file | string | Executable name or path. |
args | readonly string[] | Argument list passed to file (max 4096 entries). |
options | ChildProcessOptions | Execution options. |
callback | (error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => void | Node-style callback invoked with (error, stdout, stderr) upon completion. |
voidTypeError— synchronously whenfileoroptionshave invalid types, orencodingis unknown (ERR_UNKNOWN_ENCODING); synchronously when arguments or options have invalid types, orencodingis unknown (ERR_UNKNOWN_ENCODING); synchronously whenfileis not a string; synchronously whenfileorargshave invalid types.RangeError— synchronously whenenvlimits are exceeded ortimeout/maxBufferis invalid; synchronously whenargs/envlimits are exceeded ortimeout/maxBufferis invalid; synchronously whenargsexceeds 4096 entries.Error— synchronously withcode = "ERR_UNSUPPORTED"ifshellis enabled; rejects asynchronously on non-zero exit, timeout (ETIMEDOUT), or spawn failure (ENOENT,EACCES,EIO).
import { Buffer } from "node:buffer";import { execFile } from "node:child_process";
const { stdout } = await execFile("echo", ["hello"], { encoding: "utf8" });console.log( typeof stdout === "string" ? stdout.trim() : Buffer.from(stdout).toString("utf8").trim(),);Interfaces
Section titled “Interfaces”ChildProcessOptions
Section titled “ChildProcessOptions”export interface ChildProcessOptions {
cwd?: string;
env?: Record<string, string | undefined>;
input?: string | Uint8Array | ArrayBufferView;
encoding?: TextEncoding | "buffer" | null;
timeout?: number;
maxBuffer?: number;
}
Options controlling working directory, environment, stdin, encoding, and limits.
| Property | Type | Description | Since |
|---|---|---|---|
cwd optional | string | Working directory for the child process (inherits parent cwd when omitted). | v0.0.0-alpha.0 |
env optional | Record<string, string | undefined> | Environment key/value map applied to the child process (max 4096 entries). When provided, replaces the inherited environment with the specified entries. | v0.0.0-alpha.0 |
input optional | string | Uint8Array | ArrayBufferView | Data written to the child process standard input before closing stdin. | v0.0.0-alpha.0 |
encoding optional | TextEncoding | "buffer" | null | Output encoding for stdout and stderr ("buffer" or null returns Buffer / Uint8Array bytes). | v0.0.0-alpha.0 |
timeout optional | number | Maximum execution duration in milliseconds before killing and reaping the child process. | v0.0.0-alpha.0 |
maxBuffer optional | number | Maximum bytes captured per output stream (stdout and stderr, default 10 MiB). | v0.0.0-alpha.0 |
SpawnSyncReturns
Section titled “SpawnSyncReturns”export interface SpawnSyncReturns<T = Uint8Array | string> {
status: number | null;
signal: string | null;
stdout: T;
stderr: T;
error?: Error;
timedOut: boolean;
}
Result returned by spawnSync.
| Property | Type | Description | Since |
|---|---|---|---|
status | number | null | Process exit code, or null if terminated by a signal or spawn failed. | v0.0.0-alpha.0 |
signal | string | null | Signal name (such as "SIGKILL") if terminated by a signal, otherwise null. | v0.0.0-alpha.0 |
stdout | T | Captured standard output (decoded string or Buffer bytes depending on options.encoding). | v0.0.0-alpha.0 |
stderr | T | Captured standard error (decoded string or Buffer bytes depending on options.encoding). | v0.0.0-alpha.0 |
error optional | Error | Error object populated when spawning failed or the child process timed out. | v0.0.0-alpha.0 |
timedOut | boolean | true when the child exceeded options.timeout and was terminated. | v0.0.0-alpha.0 |
Type Aliases
Section titled “Type Aliases”TextEncoding
Section titled “TextEncoding”export type TextEncoding = "utf8" | "utf-8" | "hex" | "base64" | "base64url" | "latin1" | "ascii";
Supported text encodings for child process output.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** Synchronously spawn a child process. @since 0.0.0-alpha.0 */
spawnSync: typeof spawnSync;
/** Synchronously execute a file and return stdout. @since 0.0.0-alpha.0 */
execFileSync: typeof execFileSync;
/** Asynchronously execute a file and return stdout/stderr. @since 0.0.0-alpha.0 */
execFile: typeof execFile;
};
export default _default;
execFile is available in vjs (V8) only and omitted from the default export in vjs-lite.Default export object providing { spawnSync, execFileSync, execFile }.
