Node js readline不断评估输入并返回错误

Node js readline keep evaluating the input and returning a error

我 运行 在 Linux 终端上 运行 节点时 Node js readline 模块的奇怪行为。该模块确实有效,但它也会尝试评估用户输入,就像它是一条语句一样,返回引用错误。这只是我进行的一个简单测试,但无法正常工作……而且我已经尝试了很多教程。这是我的代码:

'nodeReadlineTeste':function(){

var readline = require('readline');

//without terminal:false I was getting double lettering on terminal input...
var rl = readline.createInterface({input:process.stdin,output:process.stdout,terminal:false});

rl.question('What is your name? ',function(resp){getIt(resp)});

//this function is to bypass NODE assyncronous nature... I was getting the log before even answering.
function getIt (resp){console.log(resp); rl.close()};

},

//当运行这个方法并响应我的名字我得到这个结果:

> global.nodeReadlineTeste()
What is your name? undefined
> Daniel
ReferenceError: Daniel is not defined
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
at REPLServer.Interface._onLine (readline.js:279:10)
at REPLServer.Interface._line (readline.js:626:8)
> Daniel

难道 readline 不应该完全避免这种情况吗?也许我犯了一些简单的错误,如果是这样请帮我找到它。提前谢谢你。

您正在尝试 运行 REPL 中的此代码。 将此代码保存到单独的 <your_file_name>.js 和 运行 node <your_file_name>.js

问题是您在 REPL 中尝试 运行 这段代码,当您在下一行节点中键入 Daniel 试图查找名称为 Daniel.[=14 的变量时=]