mirror-rhinstaller-node-cache/sync-child-process
2026-07-22 07:15:03 +00:00
..
dist 70a54663980edde26ebea82117fe36be1872d66a4e0b8026c243e93fe6b54685 package.json 2026-07-22 07:15:03 +00:00
LICENSE 70a54663980edde26ebea82117fe36be1872d66a4e0b8026c243e93fe6b54685 package.json 2026-07-22 07:15:03 +00:00
package.json 70a54663980edde26ebea82117fe36be1872d66a4e0b8026c243e93fe6b54685 package.json 2026-07-22 07:15:03 +00:00
README.md 70a54663980edde26ebea82117fe36be1872d66a4e0b8026c243e93fe6b54685 package.json 2026-07-22 07:15:03 +00:00

sync-child-process

This package exposes a SyncChildProcess class that allows Node.js to run a subprocess synchronously and interactively.

API Docs

Usage

Use new SyncChildProcess() to start running a subprocess. This supports the same API as child_process.spawn() other than a few options. You can send input to the process using process.stdin, and receive events from it (stdout, stderr, or exit) using process.next(). This implements the iterator protocol, but not the iterable protocol because it's intrinsically stateful.

import {SyncChildProcess} from 'sync-child-process';
// or
// const {SyncChildProcess} = require('sync-child-process');

const node = new SyncChildProcess('node', ['--interactive']);

for (;;) {
  if (node.next().value.data.toString().endsWith('> ')) break;
}

node.stdin.write("41 * Math.pow(2, 5)\n");
console.log((node.next().value.data.toString().split("\n")[0]));
node.stdin.write(".exit\n");
console.log(`Node exited with exit code ${node.next().value.code}`);

Why synchrony?

See the sync-message-port documentation for an explanation of why running code synchronously can be valuable even in an asynchronous ecosystem like Node.js

Why not child_process.spawnSync()?

Although Node's built-in child_process.spawnSync() function does run synchronously, it's not interactive. It only returns once the process has run to completion and exited, which means it's not suitable for any long-lived subprocess that interleaves sending and receiving data, such as when using the embedded Sass protocol.


Disclaimer: this is not an official Google product.