Saltar al contenido

Referencia de la API node:child_process

Subprocesos síncronos y asíncronos, y workers bifurcados con IPC

En esta página
Open in ChatGPTOpen in Claude

Los auxiliares de subprocesos asíncronos y síncronos funcionan en scripts normales. Un hijo en ejecución mantiene vivo el script hasta su evento close:

import { spawn, execFileSync } from "node:child_process";

const child = spawn("echo", ["hello"]);
child.stdout.on("data", (data) => console.log(String(data).trim())); // hello
child.on("close", (code) => console.log("exit", code)); // exit 0

const output = execFileSync(process.execPath, [
  "--snippet",
  'process.stdout.write("hi")',
]);
console.log(output.toString()); // hi

Las exportaciones disponibles son spawn, spawnSync, exec, execSync, execFile, execFileSync, fork y ChildProcess. spawnSync devuelve el objeto habitual con pid, status, signal, stdout, stderr y output. fork() inicia otro script de Elide con un canal IPC: child.send() y process.send() intercambian mensajes, que se entregan como eventos message.

La creación de procesos se controla con --allow-run[=CMDS] y --deny-run <CMDS> cuando las opciones de sandbox están activas.

Reference

Status: Full · Not measured · since 1.1.0

Exports

ExportKindStatusNotes
exec(), execSync()functionFullExecutes a command in a shell
execFile(), execFileSync()functionFullExecutes a file without a shell
spawn(), spawnSync()functionFullSpawns a new process using the given command
fork()functionFullSpawns a new Node.js process using the given module path

¿Te resultó útil esta página?