2

So I'm trying to pipe two Node based js scripts together, which works as expected doing something like this.

How to pipe Node.js scripts together using the Unix | pipe (on the command line)?

essentially

$ ./a.js | ./b.js

The pipe works fine as long as the only thing output to the next script is valid JSON (for example). But I would like to see some debug logs in the first script (ideally without using the popular debug module). Funny enough, I know the debug module will do this without sending unwanted data to the pipe. How does it do that? I'd rather not dig into their code to see (lazy).

Seems like console.log, and console.error both use process.stdout/err so if I log something out, I end up mucking up the pipe.

Difference between "process.stdout.write" and "console.log" in node.js?

Is there a way to use a different tty socket or something? No idea where to start.

4m1r
  • 12,234
  • 9
  • 46
  • 58

1 Answers1

2

Looks like debug module on npm writes to stderr instead:

By default debug will log to stderr

/**
 * Invokes `util.format()` with the specified arguments and writes to stderr.
 */

function log(...args) {
    return process.stderr.write(util.format(...args) + '\n');
}
Ivar
  • 4,350
  • 2
  • 27
  • 29