Skip to content

Instantly share code, notes, and snippets.

@joeylin
Created February 8, 2014 07:44
Show Gist options
  • Save joeylin/8878137 to your computer and use it in GitHub Desktop.
Save joeylin/8878137 to your computer and use it in GitHub Desktop.
nodejs input password in Terminal
function askPassword(callback) {
var stdin = process.openStdin(),
stdio = process.binding("stdio");
stdio.setRawMode();
console.log('Enter your password:');
var password = "";
stdin.on("data", function(c) {
c = c + "";
switch (c) {
case "\n": case "\r": case "\u0004":
stdio.setRawMode(false);
stdin.pause();
callback(password);
break;
case "\u0003":
process.exit();
break;
default:
password += c;
break;
}
});
}
@sweethuman
Copy link

Thank you! This was really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment