Why is my Node child process that I created via spawn() hanging?

北城余情 提交于 2019-11-30 20:14:05

As it turns out once the stderr buffer exceeds 24kb you must be reading from it or you not see any events for completion. Possible workarounds:

  1. Set the stdio option on the spawn call.

    spawn('git', ['push', 'origin', 'master'], {stdio: 'ignore'});
    

    See Node ChildProcess doc for all of the possibilities - there are lots.

  2. Add an on(data) handler.

    var git = spawn('git', ['push', 'origin', 'master']);
    ...
    git.stderr.on('data', function(data) {
      // do something with it
    });
    
  3. Pipe it to stdout / stderr. This may be too verbose for your application but including it for completeness.

    var git = spawn('git', ['push', 'origin', 'master']);
    ...
    git.stderr.pipe(process.stderr);
    git.stdout.pipe(process.stdout);
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!