How to get the output from NodeJS when use the child_process.spawn to launch a bat on windows OS, and the bat will open a new terminal

If the bat only run one terminal ,we can got the stdout, but we will failded if this open a new window.

    var terminal = require('child_process').spawn('aa.bat');
    console.log('Starting..terminal.pid.', terminal.pid, "process.pid", process.pid);
    terminal.stdout.on('data', function(data) {
        console.log('stdout:',data);
    });
    terminal.stderr.on('data', function(data) {
        console.log('stderr:',data);
    });
    terminal.on('uncaughtException', function(err) {
        console.log('Caught exception: ' + err);
    });
    terminal.on('exit', function(code) {
        console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
        console.log('child process', process.pid, 'exited with code ' + code);
    });

presumed the bat file like this

start  cmd 

if we change it to

start /b cmd

this will not open a new terminal ,the nodeJs will work

It's difficult to get another process stdout from NodeJS on windows os, at most time it open an new terminal window. but you can use an bash to launch it.

1.The bash.exe you can download from http://www.steve.org.uk/Software/bash/ then the code maybe like this:

    var terminal = require('child_process');
    function start() {
            if (process.platform.trim() !== 'win32') {
                terminal = terminal.spawn('bash');
                console.log('This is not the win32 plantform ,please confirm the bash woking!');
            } else {
                terminal = terminal.spawn('./bash-2.03/bash.exe');
            }
            // !!!must append the ./
            //terminal.stdin.write('./aa.exe');
            terminal.stdin.write('./aa.bat');
            terminal.stdin.end();
        };
        start();
        terminal.stdout.on('data', function(data) {
            console.log(data + "");
        });
        terminal.stdout.on('error', function(data) {
            console.log('error:\n' + data);
        });
        terminal.on('uncaughtException', function(err) {
            console.log('Caught exception: ' + err);
        });
        terminal.on('exit', function(code) {
            console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
            console.log('child process', process.pid, 'exited with code ' + code);
        });