Les utilitaires de sous-processus asynchrones et synchrones fonctionnent tous
deux dans des scripts ordinaires. Un enfant en cours d’exécution maintient le
script en vie jusqu’à son événement 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()); // hiLes exports disponibles sont spawn, spawnSync, exec, execSync,
execFile, execFileSync, fork et ChildProcess. spawnSync renvoie
l’objet habituel avec pid, status, signal, stdout, stderr et output.
fork() démarre un autre script Elide avec un canal IPC : child.send() et
process.send() échangent des messages, livrés sous forme d’événements
message.
La création de processus est contrôlée par --allow-run[=CMDS] et
--deny-run <CMDS> lorsque des options de bac à sable sont actives.
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 |