Search code examples
node.jsstreamstdiochild-processspawn

Child stdout set to "inherit" and "onData" listener


In a nodejs application I need to spawn a child process with stdio set to "inherit" mode, so basically:

var child = spawn('some/command', [], {
  stdio: [process.stdin, process.stdout, process.stderr]
});

This is quite important because I am expecting the child process to write to it's stdout in raw mode and I want this "raw output" visible to my users as well. At the same time I want to parse a regular output from that process. Normally I would do:

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

but in this case, there's no child.stdout. The second guess is to use process.stdout, but frankly

process.stdout.on('data', function () {
  // ...
});

is not working as well. I am wondering if there's any other way to capture that data?


Solution

  • You could use a module like pty that would allow you create a "fake tty" that you can read/write from/to.