【问题标题】:Ending REPL server without having instance在没有实例的情况下结束 REPL 服务器
【发布时间】:2013-12-05 22:55:26
【问题描述】:

如果您不通过命令行运行脚本,节点会自动进入 REPL 模式。我想退出 REPL 模式。 REPLServer 类有一个 close 方法,但我无法调用它,因为我无权访问 repl.start 返回的实例,该节点在内部使用该节点(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]');
  });
}

【问题讨论】:

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


    【解决方案1】:

    你可以通过你的程序退出 REPL:

    repl.rli.close();
    

    这是REPL exit command 在您输入.exit 时所做的。

    【讨论】:

      【解决方案2】:

      有很多方法可以退出以node 命令开始的 REPL。这里有一些:

      1. .exit
      2. process.exit();
      3. process.kill()
      4. process.kill(process.pid);

      由于您使用的是终端,您还可以通过键入 CTRL+C 两次来发送SIGINT

      【讨论】:

        猜你喜欢
        • 2016-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多