Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
Async execFile is V8-only (vjs). spawnSync and execFileSync run on both vjs and vjs-lite.
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());
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Overload 1 — spawnSync(command, args?, options?)
NameTypeDescription
commandstringExecutable name or path.
args optionalreadonly string[]Argument list passed to command (max 4096 entries).
options optionalChildProcessOptions & { 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.
Overload 2 — spawnSync(command, args, options)

Synchronously spawn command returning string-decoded stdout/stderr.

NameTypeDescription
commandstringExecutable name or path.
argsreadonly string[] | undefinedArgument list passed to command (max 4096 entries), or undefined.
optionsChildProcessOptions & { 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.
Overload 3 — spawnSync(command, options?)

Synchronously spawn command with options.

NameTypeDescription
commandstringExecutable name or path.
options optionalChildProcessOptionsExecution options without an explicit args array.
SpawnSyncReturns<Uint8Array | string> — Captured exit status, signal, stdout, stderr, optional error, and timedOut flag.
Exceptions
  • TypeError — when arguments or options have invalid types, or encoding is unknown (ERR_UNKNOWN_ENCODING).
  • RangeError — when args/env limits are exceeded or timeout/maxBuffer is invalid; when env limits are exceeded or timeout/maxBuffer is invalid.
  • Error — with code = "ERR_UNSUPPORTED" if shell is enabled in options.
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());
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Overload 1 — execFileSync(file, args?, options?)
NameTypeDescription
filestringExecutable name or path.
args optionalreadonly string[]Argument list passed to file (max 4096 entries).
options optionalChildProcessOptions & { encoding?: "buffer" | null }Execution options with binary output.
Uint8Array — Captured stdout bytes.
Overload 2 — execFileSync(file, args, options)

Synchronously execute file returning string-decoded stdout.

NameTypeDescription
filestringExecutable name or path.
argsreadonly string[] | undefinedArgument list passed to file (max 4096 entries), or undefined.
optionsChildProcessOptions & { encoding: TextEncoding }Execution options specifying a TextEncoding for stdout.
string — Decoded stdout string.
Overload 3 — execFileSync(file, options?)

Synchronously execute file with options.

NameTypeDescription
filestringExecutable name or path.
options optionalChildProcessOptionsExecution options without an explicit args array.
Uint8Array | string — Captured stdout as Uint8Array or decoded string.
Exceptions
  • TypeError — when arguments or options have invalid types, or encoding is unknown (ERR_UNKNOWN_ENCODING).
  • RangeError — when args/env limits are exceeded or timeout/maxBuffer is invalid; when env limits are exceeded or timeout/maxBuffer is invalid.
  • Error — on non-zero exit, signal termination, timeout (ETIMEDOUT), spawn failure (ENOENT, EACCES, EIO), or ERR_UNSUPPORTED if shell is enabled.
import { execFileSync } from "node:child_process";
const out = execFileSync("echo", ["hello"], { encoding: "utf8" });
console.log(out.trim());
functionSince v0.0.0-alpha.0vjs only
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).

Overload 1 — execFile(file, options?)
NameTypeDescription
filestringExecutable name or path.
options optionalChildProcessOptionsExecution options.
Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }> — Promise resolving to { stdout, stderr }, or rejecting on spawn failure, timeout, or non-zero exit.
Overload 2 — execFile(file, args?, options?)

Execute file asynchronously with args and optional options, returning a Promise.

NameTypeDescription
filestringExecutable name or path.
args optionalreadonly string[]Argument list passed to file (max 4096 entries).
options optionalChildProcessOptionsExecution options.
Promise<{ stdout: Uint8Array | string; stderr: Uint8Array | string }> — Promise resolving to { stdout, stderr }, or rejecting on spawn failure, timeout, or non-zero exit.
Overload 3 — execFile(file, callback)

Execute file asynchronously and invoke callback(error, stdout, stderr).

NameTypeDescription
filestringExecutable name or path.
callback(error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => voidNode-style callback invoked with (error, stdout, stderr) upon completion.
void
Overload 4 — execFile(file, args, callback)

Execute file asynchronously with args and invoke callback(error, stdout, stderr).

NameTypeDescription
filestringExecutable name or path.
argsreadonly string[]Argument list passed to file (max 4096 entries).
callback(error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => voidNode-style callback invoked with (error, stdout, stderr) upon completion.
void
Overload 5 — execFile(file, args, options, callback)

Execute file asynchronously with args and options, invoking callback(error, stdout, stderr).

NameTypeDescription
filestringExecutable name or path.
argsreadonly string[]Argument list passed to file (max 4096 entries).
optionsChildProcessOptionsExecution options.
callback(error: Error | null, stdout: Uint8Array | string, stderr: Uint8Array | string) => voidNode-style callback invoked with (error, stdout, stderr) upon completion.
void
Exceptions
  • TypeError — synchronously when file or options have invalid types, or encoding is unknown (ERR_UNKNOWN_ENCODING); synchronously when arguments or options have invalid types, or encoding is unknown (ERR_UNKNOWN_ENCODING); synchronously when file is not a string; synchronously when file or args have invalid types.
  • RangeError — synchronously when env limits are exceeded or timeout/maxBuffer is invalid; synchronously when args/env limits are exceeded or timeout/maxBuffer is invalid; synchronously when args exceeds 4096 entries.
  • Error — synchronously with code = "ERR_UNSUPPORTED" if shell is 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(),
);
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
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.

Properties
PropertyTypeDescriptionSince
cwd optionalstringWorking directory for the child process (inherits parent cwd when omitted).v0.0.0-alpha.0
env optionalRecord<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 optionalstring | Uint8Array | ArrayBufferViewData written to the child process standard input before closing stdin.v0.0.0-alpha.0
encoding optionalTextEncoding | "buffer" | nullOutput encoding for stdout and stderr ("buffer" or null returns Buffer / Uint8Array bytes).v0.0.0-alpha.0
timeout optionalnumberMaximum execution duration in milliseconds before killing and reaping the child process.v0.0.0-alpha.0
maxBuffer optionalnumberMaximum bytes captured per output stream (stdout and stderr, default 10 MiB).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface SpawnSyncReturns<T = Uint8Array | string> {
  status: number | null;
  signal: string | null;
  stdout: T;
  stderr: T;
  error?: Error;
  timedOut: boolean;
}

Result returned by spawnSync.

Properties
PropertyTypeDescriptionSince
status number | nullProcess exit code, or null if terminated by a signal or spawn failed.v0.0.0-alpha.0
signal string | nullSignal name (such as "SIGKILL") if terminated by a signal, otherwise null.v0.0.0-alpha.0
stdout TCaptured standard output (decoded string or Buffer bytes depending on options.encoding).v0.0.0-alpha.0
stderr TCaptured standard error (decoded string or Buffer bytes depending on options.encoding).v0.0.0-alpha.0
error optionalErrorError object populated when spawning failed or the child process timed out.v0.0.0-alpha.0
timedOut booleantrue when the child exceeded options.timeout and was terminated.v0.0.0-alpha.0
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type TextEncoding = "utf8" | "utf-8" | "hex" | "base64" | "base64url" | "latin1" | "ascii";

Supported text encodings for child process output.

defaultSince v0.0.0-alpha.0vjs & vjs-lite
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;
vjs-lite parity: execFile is available in vjs (V8) only and omitted from the default export in vjs-lite.

Default export object providing { spawnSync, execFileSync, execFile }.