The easiest way would be process.stdout.isTTY
(0.8 +):
$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
(example from the official documentation)
Alternatively you can use the tty
module for finer grained control:
if (require('tty').isatty(1)) {
// terminal
}