Search code examples
node.jstty

how to write to the terminal without writing to process.stdout or process.stderr?


I would like to log things to the terminal without writing them to process.stdout or process.stderr so that piping the process doesn't end up being polluted by those logs.

That's very much the equivalent of echo "hello" > /dev/tty in bash (see this question), but I can't find how to directly access /dev/tty from node.


Solution

  • Just open it as a file and write to it :)

    var fs  = require('fs');
    var tty = fs.createWriteStream('/dev/tty');
    
    console.log('hello');
    tty.write('foo\n');
    tty.write('bar\n');