1

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.

Community
  • 1
  • 1
maxlath
  • 1,804
  • 15
  • 24

2 Answers2

6

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');
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Can this be used for windows as well, or is there a windows-equivalent of this? – Automatico Jan 13 '23 at 19:15
  • @Automatico I have no idea, but should be easy to test :D If it doesn't work, perhaps `fs.createWriteStream('CON')` does (but I don't use Windows so can't test). – robertklep Jan 14 '23 at 07:54
0

so I found a way, but that could be considered cheating ;)

var exec = require('child_process').exec
process.stdout.write('writing to stdout')
exec("echo 'writing to the terminal without writing to stdout' > /dev/tty")
maxlath
  • 1,804
  • 15
  • 24