readline.createInterface(options)


  • options <Object>
    • input <stream.Readable> 要监听的可读流。 此选项是必需的。
    • output <stream.Writable> 要将逐行读取的数据写入的可写流。
    • completer <Function> 可选的用于制表符自动补全的函数。
    • terminal <boolean> 如果 inputoutput 流应该被视为终端,并且写入了 ANSI/VT100 转义码,则为 true默认值: 在实例化时检查 output 流上的 isTTY
    • history <string[]> 历史行的初始列表。 仅当 terminal 由用户或内部的 output 检查设置为 true 时,此选项才有意义,否则历史缓存机制根本不会初始化。 默认值: []
    • historySize <number> 保留的最大历史行数。 要禁用历史记录,则将此值设置为 0。 仅当 terminal 由用户或内部的 output 检查设置为 true 时,此选项才有意义,否则历史缓存机制根本不会初始化。 默认值: 30
    • removeHistoryDuplicates <boolean> 如果为 true,则当添加到历史列表的新输入行与旧输入行重复时,这将从列表中删除旧行。 默认值: false
    • prompt <string> 要使用的提示字符串。 默认值: '> '
    • crlfDelay <number> 如果 \r\n 之间的延迟超过 crlfDelay 毫秒,则 \r\n 都将被视为单独的行尾输入。 crlfDelay 将被强制为不小于 100 的数字。 它可以设置为 Infinity,在这种情况下,\r 后跟 \n 将始终被视为单个换行符(这对于具有 \r\n 行分隔符的文件读取可能是合理的)。 默认值: 100
    • escapeCodeTimeout <number> readline 将等待字符的时长(当以毫秒为单位读取不明确的键序列时,既可以使用目前读取的输入形成完整的键序列,又可以采用额外的输入来完成更长的键序列)。 默认值: 500
    • tabSize <integer> 一个制表符等于的空格数(最小为 1)。 默认值: 8
    • signal <AbortSignal> 允许使用中止信号关闭接口。 中止信号将在内部调用接口上的 close
  • 返回: <readline.Interface>

readline.createInterface() 方法创建新的 readline.Interface 实例。

const readline = require('node:readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

一旦创建了 readline.Interface 实例,则最常见的场景就是监听 'line' 事件:

rl.on('line', (line) => {
  console.log(`Received: ${line}`);
});

如果此实例的 terminaltrue,则如果它定义了 output.columns 属性,并且如果或当列发生变化时(process.stdout 会当其是终端时自动执行此操作)在 output 上触发 'resize' 事件,则 output 流将获得最佳的兼容性。

当使用 stdin 作为输入创建 readline.Interface 时,则程序在收到 EOF 字符之前不会终止。 要在不等待用户输入的情况下退出,则调用 process.stdin.unref()

  • options <Object>
    • input <stream.Readable> The Readable stream to listen to. This option is required.
    • output <stream.Writable> The Writable stream to write readline data to.
    • completer <Function> An optional function used for Tab autocompletion.
    • terminal <boolean> true if the input and output streams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Default: checking isTTY on the output stream upon instantiation.
    • history <string[]> Initial list of history lines. This option makes sense only if terminal is set to true by the user or by an internal output check, otherwise the history caching mechanism is not initialized at all. Default: [].
    • historySize <number> Maximum number of history lines retained. To disable the history set this value to 0. This option makes sense only if terminal is set to true by the user or by an internal output check, otherwise the history caching mechanism is not initialized at all. Default: 30.
    • removeHistoryDuplicates <boolean> If true, when a new input line added to the history list duplicates an older one, this removes the older line from the list. Default: false.
    • prompt <string> The prompt string to use. Default: '> '.
    • crlfDelay <number> If the delay between \r and \n exceeds crlfDelay milliseconds, both \r and \n will be treated as separate end-of-line input. crlfDelay will be coerced to a number no less than 100. It can be set to Infinity, in which case \r followed by \n will always be considered a single newline (which may be reasonable for reading files with \r\n line delimiter). Default: 100.
    • escapeCodeTimeout <number> The duration readline will wait for a character (when reading an ambiguous key sequence in milliseconds one that can both form a complete key sequence using the input read so far and can take additional input to complete a longer key sequence). Default: 500.
    • tabSize <integer> The number of spaces a tab is equal to (minimum 1). Default: 8.
    • signal <AbortSignal> Allows closing the interface using an AbortSignal. Aborting the signal will internally call close on the interface.
  • Returns: <readline.Interface>

The readline.createInterface() method creates a new readline.Interface instance.

const readline = require('node:readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

Once the readline.Interface instance is created, the most common case is to listen for the 'line' event:

rl.on('line', (line) => {
  console.log(`Received: ${line}`);
});

If terminal is true for this instance then the output stream will get the best compatibility if it defines an output.columns property and emits a 'resize' event on the output if or when the columns ever change (process.stdout does this automatically when it is a TTY).

When creating a readline.Interface using stdin as input, the program will not terminate until it receives an EOF character. To exit without waiting for user input, call process.stdin.unref().