У звичайних скриптах працюють як асинхронні, так і синхронні допоміжні засоби
для підпроцесів. Запущений дочірній процес підтримує роботу скрипта до своєї
події 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Доступні експорти: spawn, spawnSync, exec, execSync, execFile,
execFileSync, fork і ChildProcess. spawnSync повертає звичний об’єкт із
pid, status, signal, stdout, stderr і output. fork() запускає інший
скрипт Elide з каналом IPC: child.send() і process.send() обмінюються
повідомленнями, які доставляються як події message.
Коли активні прапорці пісочниці, створення процесів контролюють
--allow-run[=CMDS] і --deny-run <CMDS>.
Reference
Status: Full · Not measured · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
exec(), execSync() | function | Full | Executes a command in a shell |
execFile(), execFileSync() | function | Full | Executes a file without a shell |
spawn(), spawnSync() | function | Full | Spawns a new process using the given command |
fork() | function | Full | Spawns a new Node.js process using the given module path |