Node.js v18.12.1 documentation


Table of contents

TTY#

Stability: 2 - Stable

Source Code: lib/tty.js

The node:tty module provides the tty.ReadStream and tty.WriteStream classes. node:tty模块提供了tty.ReadStreamtty.WriteStream类。In most cases, it will not be necessary or possible to use this module directly. However, it can be accessed using:在大多数情况下,没有必要也不可能直接使用此模块。但是,可以使用以下方式访问:

const tty = require('node: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. 当Node.js检测到它是在附加文本终端(“TTY”)的情况下运行时,process.stdin默认情况下将初始化为tty.ReadStream的实例,process.stdoutprocess.stderr默认情况下都将是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.js是否在TTY上下文中运行的首选方法是检查process.stdout.isTTY属性的值是否为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.在大多数情况下,应用程序几乎没有理由手动创建tty.ReadStreamtty.WriteStream类的实例。