tty 终端


稳定性: 2 - 稳定

源代码: lib/tty.js

tty 模块提供了 tty.ReadStreamtty.WriteStream 类。 在大多数情况下,没有必要或不可能直接使用此模块。 但是,可以使用以下方式访问它:

const tty = require('tty');

确定 Node.js 是否在终端上下文中运行的首选方法是检查 process.stdout.isTTY 属性的值是否为 true

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false

在大多数情况下,应用程序几乎没有理由手动创建 tty.ReadStreamtty.WriteStream 类的实例。

Stability: 2 - Stable

Source Code: lib/tty.js

The tty module provides the tty.ReadStream and tty.WriteStream classes. In most cases, it will not be necessary or possible to use this module directly. However, it can be accessed using:

const tty = require('tty');

When Node.js detects that it is being run with a text terminal ("TTY") attached, process.stdin will, by default, be initialized as an instance of tty.ReadStream and both process.stdout and process.stderr will, by default be instances of tty.WriteStream. The preferred method of determining whether Node.js is being run within a TTY context is to check that the value of the process.stdout.isTTY property is true:

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false

In most cases, there should be little to no reason for an application to manually create instances of the tty.ReadStream and tty.WriteStream classes.