【发布时间】: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