Exec : display stdout “live”

前端 未结 9 1166
你的背包
你的背包 2020-11-29 15:08

I have this simple script :

var exec = require(\'child_process\').exec;

exec(\'coffee -cw my_file.coffee\', function(error, stdout, stderr) {
    console.lo         


        
9条回答
  •  遥遥无期
    2020-11-29 15:28

    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.

提交回复
热议问题