Test whether the actual output is a terminal or not in node.js

回眸只為那壹抹淺笑 提交于 2019-11-30 17:09:25
loganfsmyth

Similarly to the bash examples you link to, Node has the 'tty' module to deal with this.

To check if output is redirected, you can use the 'isatty' method. Docs here: http://nodejs.org/docs/v0.5.0/api/tty.html#tty.isatty

For example to check if stdout is redirected:

var tty = require('tty');
if (tty.isatty(process.stdout.fd)) {
  console.log('not redirected');
}
else {
  console.log('redirected');
}

Update

In new versions of Node (starting from 0.12.0), the API provides a flag on stdout so you can just do this:

if (process.stdout.isTTY) {
  console.log('not redirected');
}
else {
  console.log('redirected');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!