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

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 gives me no way out

+5
source share
1 answer

If you do not use 'pipe' , then childProcess.output will not contain an 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 sorted as indicated in the documentation for child.stdout and elsewhere, but it is not entirely straightforward. (In any case, if you want to improve it, open a Node.js repo port transfer request.)

+6
source

Source: https://habr.com/ru/post/1244066/


All Articles