I use the node child_process API
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
var child = child_process.spawn(cmd, val, options);
from the child I use the following
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
Can I add inside those pipe event some code inside like console.log?
like for example maybe with prototype
child.on('error', function(err) {
console.log(err);
});
update
What I need that is to listen to this childProcess.stderr.pipe(process.stderr);
and In case I got and error do process.exit(1)
when I try something like I got error
child.stderr.pipe(function () {
console.log("im here");
process.stderr;
process.exit(1);
}
);
UPDATE2
I try the following
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE");
console.log('test: ' + data);
reject(data);
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
child.on('error', function (err) {
console.log("IM HERE");
console.log(err);
});
child.stderr.on('error', function (err) {
console.log("IM HERE");
console.log("my Erorr");
process.stderr.emit('error', err);
});
child.stdout.on('data', function (buf) {
console.log("IM HERE");
console.log('buf receive');
console.log(buf.toString());
});
//Just when I add the following I see the error in the log
child.stderr.pipe(process.stderr)
Non of the console.log("im here") is printed in case of error
I need somehow to listen to this pipe or maybe to extend somehow the child.stderr.pipe(process.stderr), what I need is to do process.exit(1)
in case I got error from the code statement above...
Maybe with javascript prototype but not sure how to do that...
Please assist Im stuck and I know this is not simple...
This works for me:
var child_process = require('child_process');
var cmd = 'ls';
var value = ['-z', '/usr'];
var opt = { };
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE - Error");
console.log('test: ' + data);
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
Console output:
/*
IM HERE - Error
test: ls: invalid option -- 'z'
Try 'ls --help' for more information.
IM HERE
close
*/
The issue in your side, maybe the command you're spawning doesn't use stderr?
Update
If I add process.exit()
var child_process = require('child_process');
var cmd = 'ls';
var value = ['-z', '/usr'];
var opt = { };
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE - Error");
console.log('test: ' + data);
process.exit(1); // <<<< this works as expected and exit the process asap
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
The ouput is slightly different (no close event from the child)
/*
IM HERE - Error
test: ls: invalid option -- 'z'
Try 'ls --help' for more information.
*/
You can "play" with the code here: https://tonicdev.com/shanshan/cp-spawn-piping
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With