Child process

child_process.spawn()

Starts a subprocess with streaming standard I/O. Pass an argument array and keep shell disabled for a fixed executable; wait for close before consuming complete output. Tested on Node.js 22.16.0. Example output (one line per entry): ["0 two words"].

Formal Syntax & Structure

nodejs syntaxnodejs
spawn(command[, args][, options])

Parameters & Attributes

NameTypeDescription
commandstringExecutable path.
argsstring[]Arguments.
optionsobjectProcess configuration.

Return Value

ChildProcess

Subprocess handle.

Working Example

Example Snippetnodejs
import { spawn } from 'node:child_process';import { once } from 'node:events';const c = spawn(process.execPath, ['-e', 'console.log(process.argv[1])', 'two words'], { shell: false, env: {}, stdio: ['ignore', 'pipe', 'pipe'] });let out = '';c.stdout.setEncoding('utf8').on('data', s => out += s);const [status] = await once(c, 'close');console.log(status, out.trim());
Node.js Reference