Exec : display stdout "live"
Don't use exec
. Use spawn
which is an EventEmmiter
object. Then you can listen to stdout
/stderr
events (spawn.stdout.on('data',callback..)
) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn, ls = spawn('ls', ['-lh', '/usr']);ls.stdout.on('data', function (data) { console.log('stdout: ' + data.toString());});ls.stderr.on('data', function (data) { console.log('stderr: ' + data.toString());});ls.on('exit', function (code) { console.log('child process exited with code ' + code.toString());});
exec
buffers the output and usually returns it when the command has finished executing.
exec
will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;var coffeeProcess = exec('coffee -cw my_file.coffee');coffeeProcess.stdout.on('data', function(data) { console.log(data); });
OR pipe
the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn
and the { stdio: 'inherit' }
option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone
.
Simply do this:
var spawn = require('child_process').spawn;spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.