Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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());
})();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda