1

I'm new to NodeJs and i'm used to C# were we can use Console.ReadLine();

I looked into 'readline' and the node prompt package, but it either outputs all the user input twice while entering or, with the 'terminal: false' option, does not allow us to use the backspace.

F.H.
  • 1,456
  • 1
  • 20
  • 34
  • Not exactly, i had the peculiar case that the console's output was doubled on every letter input. Probably that was an issue in conjunction with Ubuntu 18.04 – F.H. Dec 22 '21 at 11:46
  • Can you provide a [mcve] then of the code that doubles the input? Thanks. The answers here and the one you accepted are essentially the same as the suggested dupe. – ggorlen Dec 22 '21 at 16:01
  • No sorry i can't. This question was asked 5 years ago.. But as far as i remember and my comments state the code from the answer was producing the same issue. – F.H. Dec 22 '21 at 16:32

2 Answers2

4
var stdin = process.openStdin();

stdin.addListener("data", function(d) {
    console.log("your input: " + d.toString());
});

This is like you take input value

salihgungor
  • 111
  • 1
  • 7
2

There is a readline, you can use it like this:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('How are you today? ', (answer) => {
  // TODO: Log the answer in a database
  console.log(`Thank you for your valuable feedback: ${answer}`);

  rl.close();
});

Documentation is available here.

Ryan Z
  • 2,657
  • 2
  • 8
  • 20
  • @F.H. the code provided allows backspacing, and does not run into any of the issues outlined in your question. – Ryan Z Jul 16 '18 at 23:00
  • 2
    If i type 'Hello World' i get 'HHeelllloo WWoorrlldd', don't know about you but i guess it's the same right? – F.H. Jul 16 '18 at 23:02
  • I do not get that. What OS and terminal are you using? – Ryan Z Jul 16 '18 at 23:03
  • Sorry for my response then. I'm using Ubuntu 18.04. I'm referring to the output before hitting enter. – F.H. Jul 16 '18 at 23:07
  • That's a bizarre issue! It's probably an issue with the terminal then, and not with the code. See if you have a setting called echo turned on, that may be the cause. – Ryan Z Jul 16 '18 at 23:24
  • Can't find that setting. Strange, but now it makes sense that i find so little about this issue on the internet. It's a fresh installation of Ubuntu too. – F.H. Jul 17 '18 at 15:04
  • Very weird. The folks in AskUbuntu may know more about it, I don't use Ubuntu personally. – Ryan Z Jul 17 '18 at 15:06