Question

If you don't run a script via command line, node enters the REPL mode automatically. I want to exit the REPL mode. The REPLServer class has a close method but I can't call it because I don't have access to the instance returned by repl.start which node uses internally (node.js line 142) to realize its REPL mode. How can I exit the REPL mode though?

Outline from node.js line 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]');
  });
}
Was it helpful?

Solution

You can exit REPL via your program by doing:

repl.rli.close();

This is what the REPL exit command does when you enter .exit.

OTHER TIPS

There are quite a few ways to exit from a REPL started with the node command. Here's a few:

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

Since you're using a terminal, you can also send a SIGINT by typing CTRL+C twice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top