A real sync-prompt solution for node.
Python users have always add a simple easy way to grab user-input:
foo = input()
while us JavaScript users are stuck with:
process.stdin.resume()
process.stdin.on('data', function(foo) {
// ... code
});
but those days are long over! This brings an elegant prompt function to node.
Well they are other sync-prompt libraries? Hardly, here are the two:
- sync-prompt, not maintained, doesn't even work on most versions of node anymore
- prompt-sync, very flimsy, doesn't properly handle mixed stdin modes, and lacks proper support for more complex stdin reading. Attempts to mimick terminal inputs with raw-mode but breaks easily
So here's a real solution, one that actually mimicks a getline
or input
function (uses getline()
internally).
This is easy. Install using:
$ npm install --save syncprompt
Now, in your project, just do:
var prompt = require('syncprompt');
or, if you're using the fancy new import
syntax:
import prompt from 'syncprompt';
Usage couldn't be more simple. Simple do:
var name = prompt("What's your name? ");
Passwords? simply add secure: true
and you'll get a real hidden password prompt:
var passwords = prompt("Password? ", { secure: true });
A full example is the following:
var prompt = require('syncprompt');
var username = prompt("Username: ");
var password = prompt("Password: ", { secure: true });
console.log( "Username: %s\nPassword: %s", username, password );