【问题标题】:Node.js - child process with spawn: can't stream data from childNode.js - 带有 spawn 的子进程:无法从子进程流式传输数据
【发布时间】:2015-12-02 11:19:07
【问题描述】:

我想在 Windows 上生成一个子进程来打开终端 (cmd.exe)。我让一切正常,除了我无法将数据从子进程流式传输到父进程。我想在父进程中访问终端的输出。这是我的代码:

    var spawn = require('child_process').spawn;

    var child = spawn('cmd', [ '/c', 'start'], {
        cwd: '{path-to-folder}'
        });


    child.stdout.on('data', function (data) {
        console.log(data);
    });

    child.stderr.on('data', function (data) {
        console.log(data);
    });

    child.on('close', function () {

        console.log('close');
    })

我真的被困住了,所以任何帮助或提示都会很棒!提前致谢!

【问题讨论】:

    标签: node.js child-process spawn


    【解决方案1】:

    那是因为你在新的终端会话中启动了新的 shell。您只能拦截运行相同终端会话的命令的输出。

    例如,如果您将start 更改为dir,您将获得输出:

    var child = spawn('cmd', [ '/c', 'dir'], {
        cwd: '.'
        });
    child.stdout.on('data', function (data) {
        console.log(data.toString());
    });
    

    驱动器 C 中的卷没有标签。 卷序列号为 9401-94AE

    Directory of C:\Temp
    
    
    12/02/2015  01:29 PM    <DIR>          .
    12/02/2015  01:29 PM    <DIR>          ..
    12/02/2015  01:30 PM               403 test.js
                   4 File(s)     10,423,442 bytes
                   4 Dir(s)  12,869,840,896 bytes free
    
    close
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2011-07-18
      • 2013-07-25
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多