Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a module for getting user input from the command line in node.js?

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)

like image 316
Hubro Avatar asked Jan 22 '12 00:01

Hubro


People also ask

How do you ask for user input in NodeJS?

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.

Can I take input from console in JavaScript?

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.

Which method is used for getting input from users in JavaScript?

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.

What is readline () in JavaScript?

The Readline module provides a way of reading a datastream, one line at a time.


1 Answers

Search for modules here: http://eirikb.github.com/nipster/

  • Prompt: https://github.com/jesusabdullah/node-prompt
  • Progress bar: https://github.com/substack/node-multimeter
  • Colors: https://github.com/Marak/colors.js
  • Input validation: https://github.com/chriso/node-validator
  • More input validation (webbish tho): https://github.com/caolan/forms

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

like image 92
DTrejo Avatar answered Oct 20 '22 19:10

DTrejo