How can I pass stdout from one child process to another child process in nodejs?
I'm getting a cryptic error when I try to pass the stdout from on process to the stdin of another process.
// test.js
const child_process = require("child_process");
const proc = child_process.spawn("ls", ["-a"]);
const res = child_process.spawnSync("head", ["-n", "3"], {
stdio: [proc.stdout, "pipe", "pipe"],
});
console.log(res.toString("utf8"));
When I run node test.js
with node version v16.13.1
, instead of seeing the result of ls -a | head -n 3
, I get the following error:
node test.js[12920]: c:\ws\src\spawn_sync.cc:932: Assertion `0 && "invalid child stdio type"' failed.
1: 00007FF63F1230AF v8::internal::CodeObjectRegistry::~CodeObjectRegistry+112511
2: 00007FF63F0B2216 DSA_meth_get_flags+65542
3: 00007FF63F0B25D1 DSA_meth_get_flags+66497
4: 00007FF63EFD2C69 v8::internal::wasm::WasmCode::code_comments_offset+35065
...
34: 00007FF63FF63A88 v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor+14472
35: 00007FFE6C787034 BaseThreadInitThunk+20
36: 00007FFE6E642651 RtlUserThreadStart+33
Am I not allowed to mix child_process.spawn
and child_process.spawn_sync
?
This works if I change child_process.spawn_sync
to child_process.spawn
, but I need the command call to be synchronous.
Solution 1:
no, you can't mix them, choose one: sync (both spawnSync
) or async (which you can carry out synchronously via async/await
):
// all sync
const proc = child_process.spawnSync('ls', ['-a']);
const res = child_process.spawnSync('head', ['-n', '3'], {input: proc.stdout });
console.log('sync: res', res.stdout.toString('utf8'));
// all async, await result
const cp = () => {
return new Promise((resolve, reject) => {
const proc = child_process.spawn('ls', ['-a']);
const res = child_process.spawn('head', ['-n', '3']);
// pipe between processes
proc.stdout.pipe(res.stdin);
const buffers = [];
res.stdout.on('data', (chunk) => buffers.push(chunk));
res.stderr.on('data', (data) => {
console.error(`stderr: ${data.toString()}`);
reject(data.toString())
});
res.stdout.on('end', () => {
const result = (Buffer.concat(buffers)).toString();
console.log(`done, result:\n${result}`);
resolve(result);
});
});
};
// get results synchronously
(async() => {
console.log('await res:', await cp());
})();