Daniel Z A
Daniel Z A

Reputation: 13

Node js readline keep evaluating the input and returning a error

I'm running on a strange behavior from Node js readline module while running node on Linux terminal. The module does work but it also try to evaluate the user input as if it was a statement, returning a Reference Error. This is just a simple test I making, but can't get it to work... and I tried many tutorials already. Heres my code:

'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()};

},

//When running this method and responding my name I get this result:

> 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

Wasn't readline supposed to avoid exactly that? Maybe I doing some simple mistake, if so please help me find it. Thank you in advance.

Upvotes: 1

Views: 841

Answers (1)

RidgeA
RidgeA

Reputation: 1546

You are trying to run this code in REPL. Save this code into separate <your_file_name>.js and run node <your_file_name>.js

The problem is that you are trying run this code in REPL and when you are typing Daniel in next line node trying to find variable with name Daniel.

Upvotes: 1

Related Questions