Grunt产生的进程没有捕获输出

15 process spawn node.js gruntjs

我使用Grunt生成了一个进程,但是没有任何写入输出流(例如console.log)的内容正在控制台中显示.

我希望Grunt显示该过程的任何输出.

grunt.util.spawn(
  { cmd: 'node'
  , args: ['app.js']
  , opts:
      { stdio:
          [ process.stdin
          , process.stout
          , process.stderr
          ]
      }
  })
Run Code Online (Sandbox Code Playgroud)

Kyl*_*ung 35

尝试将其设置为opts: {stdio: 'inherit'}.否则你可以管道输出:

var child = grunt.util.spawn({
  cmd: process.argv[0], // <- A better way to find the node binary
  args: ['app.js']
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
Run Code Online (Sandbox Code Playgroud)

或者,如果要修改输出:

child.stdout.on('data', function(buf) {
    console.log(String(buf));
});
Run Code Online (Sandbox Code Playgroud)