javascript - 在没有实例的情况下结束 REPL 服务器

标签 javascript node.js console read-eval-print-loop

如果不通过命令行运行脚本, Node 会自动进入 REPL 模式。我想退出 REPL 模式。 REPLServer类有一个 close 方法,但我无法调用它,因为我无权访问 repl.start 返回的实例哪个 Node 在内部使用(node.js第142行)来实现其REPL模式。我怎样才能退出 REPL 模式?

node.js 第 142 行概要:

// If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
  // REPL
  var opts = {
    useGlobal: true,
    ignoreUndefined: false
  };
  if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
    opts.terminal = false;
  }
  if (parseInt(process.env['NODE_DISABLE_COLORS'], 10)) {
    opts.useColors = false;
  }
  var repl = Module.requireRepl().start(opts); // <<< line 142
  repl.on('exit', function() {
    process.exit();
  });

} else {
  // Read all of stdin - execute it.
  process.stdin.setEncoding('utf8');

  var code = '';
  process.stdin.on('data', function(d) {
    code += d;
  });

  process.stdin.on('end', function() {
    process._eval = code;
    evalScript('[stdin]');
  });
}

最佳答案

您可以通过程序退出 REPL:

repl.rli.close();

这就是REPL exit command当您输入 .exit 时执行。

关于javascript - 在没有实例的情况下结束 REPL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412513/

相关文章:

powershell - PowerShell 中的输出重定向仍然带有颜色

javascript - 当我尝试部署 React heroku 应用程序时出现无效的主机 header 错误

javascript - Node 内存使用情况和内存不足

node.js - Node.js 的用法 - 它旨在提供一个斜坡的障碍是什么?

ruby - 在固定位置打印和更新多行

java - 在eclipse中,如何调用从控制台读取的函数?

javascript - 如何在 Kendo Grid 中的 url 列中显示带有模板的窗口

json - 如何检查 JSON 数据是一个对象还是一组对象?

javascript - 如何使用正则表达式模式验证版本号

node.js - Derby.js (Racer) 访问控制