Ask coding questions

← Back to all posts
Reading Console Input using Node.js
vedprad1 (906)

Reading Console Input using Node.js

Is it possible to take in Console Input using Node.js? My way is not working.
The Repl

Answered by Pythonier (315) [earned 5 cycles]
View Answer
Pythonier (315)

It is possible, I don't code Node.js but I found this from

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout

readline.question(`What's your name?`, (name) => {
  console.log(`Hi ${name}!`)

Hope that helps!

vedprad1 (906)

@Pythonier : That's exactly what I have! However, it just displays the question and skips the input and the action after the input.

Pythonier (315)

@vedprad1 Weird, try requiring readline-sync instead. ex:

const readline = require("readline-sync");
var name = readline.question("What is your name? ");
Pythonier (315)

@vedprad1 Is this what you are looking for:

var rl = require('readline-sync');
rl.question("What do you think of node.js? ")
console.log("Thank you for your valuable feedback.");
vedprad1 (906)

@Pythonier : This seems to work! Thanks!

Pythonier (315)


If that was helpful can you please mark my comment as the answer.

DCha0tiC (0)

I know it's an old thread, but maybe you can use :

let answerText = prompt("Your question goes here");
let answerBool = confirm("Yes/No question goes here);"

It works nice with Repl console.

Source: W3Schools Js Popups