I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee). If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with
console.log()
is that it adds newlines, which can spread your spawned process output over additional lines. If you outputstdout
orstderr
withprocess.stdout.write()
instead ofconsole.log()
, then you'll get the console output from the spawned process 'as is'.I saw that solution here: Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
exec
will also return a ChildProcess object that is an EventEmitter.OR
pipe
the child process's stdout to the main stdout.