Zum Inhalt springen

API-Referenz zu node:child_process

Synchrone und asynchrone Unterprozesse sowie abgezweigte Worker mit IPC

Auf dieser Seite
Open in ChatGPTOpen in Claude

Sowohl asynchrone als auch synchrone Hilfsfunktionen für Unterprozesse funktionieren in gewöhnlichen Skripten. Ein laufender Kindprozess hält das Skript bis zu seinem Ereignis close am Leben:

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

Verfügbare Exporte sind spawn, spawnSync, exec, execSync, execFile, execFileSync, fork und ChildProcess. spawnSync gibt das vertraute Objekt mit pid, status, signal, stdout, stderr und output zurück. fork() startet ein weiteres Elide-Skript mit einem IPC-Kanal: child.send() und process.send() tauschen Nachrichten aus, die als Ereignisse message zugestellt werden.

Das Erstellen von Prozessen wird bei aktiven Sandbox-Flags über --allow-run[=CMDS] und --deny-run <CMDS> gesteuert.

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

War diese Seite hilfreich?