Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read child_process.spawnSync stdout with stdio option 'inherit'

var childProcess = cp.spawnSync(command, args, {
    cwd: process.cwd(),
    env: process.env,
    stdio: 'inherit',
    encoding: 'utf-8'
});

childProcess.output always eq [null, null, null]

process.stdout.write hook doesn't give me any output

like image 931
nitro-n Avatar asked Feb 28 '16 22:02

nitro-n


2 Answers

If you don't use 'pipe' then childProcess.output will not contain the output.

var cp = require('child_process');

var command = 'echo';
var args = ['hello', 'world'];

var childProcess = cp.spawnSync(command, args, {
    cwd: process.cwd(),
    env: process.env,
    stdio: 'pipe',
    encoding: 'utf-8'
});

console.log(childProcess.output); // [ null, 'hello world\n', '' ]

This is sorta kinda indicated in the documentation for child.stdout and elsewhere, but it's not entirely unambiguous. (By all means, if you wish to see it improved, open a pull request against the Node.js repo.)

like image 157
Trott Avatar answered Nov 17 '22 23:11

Trott


Use this for in-process displaying of progress:

var cp = require('child_process');

var command = 'echo';
var args = ['hello', 'world'];

var childProcess = cp.spawnSync(command, args, {
    cwd: process.cwd(),
    env: process.env,
    stdio: [process.stdin, process.stdout, process.stderr],
    encoding: 'utf-8'
});

So you replace string 'pipe' with the array [process.stdin, process.stdout, process.stderr].

like image 44
mvermand Avatar answered Nov 17 '22 22:11

mvermand