I am looking for a clear answer on how the stdout
and stderr
work when developing a npm cli module.
I would like to print out everything exactly as it is when I run command with child_process.spawn
.
I managed to output the git clone
command with the --progress
options. Now I want to output the npm install
command but it is printing only the final string.
Would be nice to have a clear answer on how this work in general and what are the best practices.
This works:
import * as cp from 'child_process';
const child = cp.spawn('git', ['clone', 'ssh://myrepo...', '--progress']);
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
process.stdout.write(`${chunk}`);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', (chunk) => {
process.stdout.write(`${chunk}`);
});
// BTW why git is outputing in STDERR and not in STDOUT?
However this doesn't work
const child = cp.spawn('npm', ['i', 'mymodule']);
Is there a way to make this work for each command?