Skip to content Skip to sidebar Skip to footer

How To Pipe And Save Output Of Spawnsync Process In Node.js?

I spawn some command synchronously and want two things: Pipe its stdout to process.stdout. Save the stdout into variable. I’ve written this code: var spawnSync = require('child

Solution 1:

Does this solve your problem?

var spawnSync = require('child_process').spawnSync;
var result = spawnSync('ls', [ '-l', '-a' ], {
    cwd: process.cwd(),
    env: process.env,
    stdio: 'pipe',
    encoding: 'utf-8'
});
var savedOutput = result.stdout;

console.log(String(savedOutput));

Solution 2:

I think the problem is that "spawnSync" is synchronous, and does not return control to node until the child process has exited. This means node does no processing during the execution of the child, and thus cannot see any of the intermediate output from the child.

You need to use the non-"sync" spawn. This will have additional complications in that your code is async, and you need to wait for the 'close' event to be sure the child data is collected. And you need to figure out what to do with stderr. Something like this:

const spawn = require('child_process').spawn;
const lsChild = spawn('ls', [ '-l', '-a' ]);

let savedOutput = '';

lsChild.stdout.on('data', data => {
   const strData = data.toString();
   console.log(strData);
   savedOutput += strData;
});

lsChild.stderr.on('data', data => {
   assert(false, 'Not sure what you want with stderr');
});

lsChild.on('close', code => {
   console.log('Child exited with', code, 'and stdout has been saved');
   // at this point 'savedOutput' contains all your data.
});

Solution 3:

There are a couple of possible solutions, both involving spawnSync's options.

There is a live version of this code available at https://repl.it/repls/MundaneConcernedMetric

const spawnSync = require('child_process').spawnSync;

// WITH NO OPTIONS:// stdout is here, but is encoded as 'buffer'const result_noOptions = spawnSync('ls', [ '-l', '-a' ]); // won't print anythingconsole.log(result_noOptions.stdout); // will print <Buffer ...> on completion// WITH { encoding: 'utf-8'} OPTIONS:// stdout is also here, _but only printed on completion of spawnSync_const result_encoded = spawnSync('ls', [ '-l', '-a' ], { encoding: 'utf-8' }); // won't print anythingconsole.log(result_encoded.stdout); // will print ls results only on completion// WITH { stdio: 'inherit' } OPTIONS:// there's no stdout, _because it prints immediately to your terminal_const result_inherited = spawnSync('ls', [ '-l', '-a' ], { stdio: 'inherit'}); // will print as it's processingconsole.log(result_inherited.stdout); // will print null

In addition to stdout, you can also get: pid, output, stdout, stderr, status, signal, & error.

https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options

Solution 4:

stdout was not helping to get output for spawnSync, so used this instead for storing output in a variable. I'm using Windows cmd

var cp = require('child_process');

var ls = cp.spawnSync('cmd.exe', ['/c', 'my.bat']);

    var output = result.output.toString();

Post a Comment for "How To Pipe And Save Output Of Spawnsync Process In Node.js?"