How to get the output of a spawned child_process in Node.JS? How to get the output of a spawned child_process in Node.JS? shell shell

How to get the output of a spawned child_process in Node.JS?


You can do it something like below.

    var spawn = require('child_process').spawn;    // Create a child process    var child = spawn('ls' , ['-l']);    child.stdout.on('data',        function (data) {            console.log('ls command output: ' + data);        });    child.stderr.on('data', function (data) {        //throw errors        console.log('stderr: ' + data);    });    child.on('close', function (code) {        console.log('child process exited with code ' + code);    });

Update: with spawnSync

    var spawn = require('child_process').spawnSync;    var child = spawn('ls' , ['-l','/usr']);    console.log('stdout here: \n' + child.stdout);