Node child process event listen
Asked Answered
D

2

11

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...

Discourse answered 20/1, 2016 at 12:2 Comment(0)
B
7

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

Bolster answered 29/1, 2016 at 7:43 Comment(2)
Thanks but this is not working for me :( the only thing that is print the error is child.stderr.pipe(process.stderr) the process.stderr, can I know when there is error is received so I do exit(1)?Discourse
@shopiaT child.stderr.on('data') allows you to know when an error happens. I added process.exit(1) in my code and the output is bit different.Bolster
I
0

You can subscribe to the data event(stdout or stderr) and listen it

child.stdout.on('data', function (buf) {
    console.log(buf.toString());
});

For example

const spawn = require('child_process').spawn;
const child = spawn('ping', ['google.com']);

child.stdout.on('data', function (buf) {
    console.log('buf receive');

    console.log(buf.toString());
});
Interruption answered 20/1, 2016 at 14:10 Comment(4)
can you tell that exactly not work? I added example to answerInterruption
Thanks but this is not working I need to listen to error please see my updateDiscourse
child.stderr.on('data', function (buf) {process.exit(1);});? do I understand correctly that you want to exit the program then receipt the first part of stderr?Interruption
yes I need it to exit,when I try your proposal and getting errror its not invoked... the only things which printed the error is child.stderr.pipe(process.stderr), the error are coming from process.stderr and I need somethow to detect that this is printed and do exit... is it possible ?Discourse

© 2022 - 2024 — McMap. All rights reserved.