repl.start([options])


版本历史
版本变更
v25.9.0

The handleError parameter has been added.

v24.1.0

Added the possibility to add/edit/remove multilines while adding a multiline command.

v24.0.0

The multi-line indicator is now "|" instead of "...". Added support for multi-line history. It is now possible to "fix" multi-line commands with syntax errors by visiting the history and editing the command. When visiting the multiline history from an old node version, the multiline structure is not preserved.

v13.4.0, v12.17.0

The preview option is now available.

v12.0.0

The terminal option now follows the default description in all cases and useColors checks hasColors() if available.

v10.0.0

The REPL_MAGIC_MODE replMode was removed.

v6.3.0

The breakEvalOnSigint option is supported now.

v5.8.0

The options parameter is optional now.

v0.1.91

新增于: v0.1.91

  • options <Object> | <string>
    • prompt <string> 要显示的输入提示。默认值: '> ' (后面带一个空格)。
    • input <stream.Readable> 将从中读取 REPL 输入的 Readable 流。默认值: process.stdin
    • output <stream.Writable> REPL 输出将写入的 Writable 流。默认值: process.stdout
    • terminal <boolean> 如果 true,指定 output 应被当作 TTY 终端处理。**默认值:**在实例化时检查 output 流上的 isTTY 属性的值。
    • eval <Function> 在评估每行输入时使用的函数。默认: JavaScript eval() 函数的异步封装器。eval 函数可以通过 repl.Recoverable 报错,以指示输入不完整并提示输入更多行。有关详细信息,请参阅 自定义评估函数 部分。
    • useColors <boolean> 如果 true,指定默认的 writer 函数应在 REPL 输出中包含 ANSI 颜色样式。如果提供了自定义 writer 函数,则此设置无效。**默认值:**如果 REPL 实例的 terminal 值为 true,则检查 output 流上的颜色支持。
    • useGlobal <boolean> 如果 true,指定默认的评估函数将使用 JavaScript global 作为上下文,而不是为 REPL 实例创建一个新的独立上下文。Node CLI REPL 将此值设置为 true默认值: false
    • ignoreUndefined <boolean> 如果 true,指定默认写入器在命令的返回值为 undefined 时不会输出该返回值。默认值: false
    • writer <Function> 在写入 output 之前调用以格式化每个命令输出的函数。默认值: util.inspect()
    • completer <Function> 一个可选功能,用于自定义 Tab 自动补齐。示例见 readline.InterfaceCompleter
    • replMode <symbol> 一个标志,用于指定默认求值器是否以严格模式或默认(宽松)模式执行所有 JavaScript 命令。可接受的值为:
      • repl.REPL_MODE_SLOPPY 用于在宽松模式下评估表达式。
      • repl.REPL_MODE_STRICT 用于在严格模式下评估表达式。这等同于在每条 REPL 语句前加上 'use strict'
    • breakEvalOnSigint <boolean> 当收到 SIGINT 时停止评估当前的代码片段,例如当按下 Ctrl+C 时。这不能与自定义 eval 函数一起使用。默认值: false
    • preview <boolean> 定义 repl 是否打印自动补全和输出预览。默认值: 使用默认 eval 函数时为 true,使用自定义 eval 函数时为 false。如果 terminal 为假值,则不会有预览,且 preview 的值无效。
    • handleError <Function> 此函数用于在 REPL 中自定义错误处理。它将抛出的异常作为第一个参数,并且必须同步返回以下值之一:
      • 'print' 将错误打印到输出流(默认行为)。
      • 'ignore' 跳过所有剩余的错误处理。
      • 'unhandled' 将异常视为完全未处理。在这种情况下,错误将传递给整个进程范围的异常处理程序,例如 'uncaughtException' 事件。'unhandled' 值在 REPLServer 实例已关闭的情况下可能是可取的,也可能不可取,这取决于具体的使用场景。
  • 返回:<repl.REPLServer>

repl.start() 方法创建并启动一个 repl.REPLServer 实例。

🌐 The repl.start() method creates and starts a repl.REPLServer instance.

如果 options 是一个字符串,那么它指定了输入提示:

🌐 If options is a string, then it specifies the input prompt:

import repl from 'node:repl';
// a Unix style prompt
repl.start('$ ');const repl = require('node:repl');
// a Unix style prompt
repl.start('$ ');

AltStyle によって変換されたページ (->オリジナル) /