Skip to content
Advertisement

Get user input through Node.js console

I have just started using Node.js, and I don’t know how to get user input. I am looking for the JavaScript counterpart of the python function input() or the C function gets. Thanks.

Advertisement

Answer

There are 3 options you could use. I will walk you through these examples:

(Option 1) prompt-sync: In my opinion, it is the simpler one. It is a module available on npm and you can refer to the docs for more examples prompt-sync.

JavaScript
JavaScript

(Option 2) prompt: It is another module available on npm:

JavaScript
JavaScript

(Option 3) readline: It is a built-in module in Node.js. You only need to run the code below:

JavaScript

Enjoy!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement