First of all: I don't mean parsing arguments and options from the process.argv
array, but prompting the user and handling input/output. I've looked through the Node.js official module list without finding any sections or subsections that mentions input. In fact a search for 'input' on that page only gets 1 result which has something to do with YAML.
Anyway, I suppose cli input should be asynchronous and I've solved this issue before using stdin.on('data')
which was messy to say the least. This seems like a perfect task for a node module which could come with extra goodies such as progress bars, spinners, coloured output, input validation etc.
There probably are some modules out there that does this, but I can't find any of them.
Help!!
(To clarify, the only functionality I require is the simplification of handling user input)
Using prompt-sync const prompt = require('prompt-sync')(); Notice the extra () after require() . The prompt-sync module is a function that creates prompting functions, so you need to call prompt-sync in order to get your actual prompting function.
However, what is not understandable is using JavaScript which runs only the browser, to get input from a systems command line. Doing this is possible using NodeJS. It will require us to use the readline() package. With this package, we can then accept user input and even output it to the console.
In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned.
The Readline module provides a way of reading a datastream, one line at a time.
Search for modules here: http://eirikb.github.com/nipster/
Also, if you want to write your own: http://nodejs.org/docs/latest/api/all.html#readline
#node.js
IRC welcomes you: http://webchat.freenode.net/?channels=node.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With