How to hide password in the nodejs console

0 votes

I want to hide password input. I see many answers in stackoverflow but I can't verify value if I press backspace. The condition return false.

I tried several solution to overwrite the function but I got an issue with buffer if I press backspace, I got invisible character \b.

I press : "A", backspace, "B", I have in my buffer this : "\u0041\u0008\u0042" (toString() = 'A\bB') and not "B".

I have :

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("password : ", function(password) {
    console.log("Your password : " + password);
});
Nov 30, 2020 in Node-js by kartik
• 37,510 points
4,469 views

1 answer to this question.

0 votes

Hello @kartik,

To hide your password input, you can use :

password : [-=]
password : [=-]

The code :

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.stdoutMuted = true;

rl.query = "Password : ";
rl.question(rl.query, function(password) {
  console.log('\nPassword is ' + password);
  rl.close();
});

rl._writeToOutput = function _writeToOutput(stringToWrite) {
  if (rl.stdoutMuted)
    rl.output.write("\x1B[2K\x1B[200D"+rl.query+"["+((rl.line.length%2==1)?"=-":"-=")+"]");
  else
    rl.output.write(stringToWrite);
};

This sequence "\x1B[2K\x1BD" uses two escapes sequences :

  • Esc [2K : clear entire line.
  • Esc D : move/scroll window up one line.

Hope it helps!!

To know more about it, enroll in Node.js training today.

Thank You!!

answered Nov 30, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,060 points
3,392 views
0 votes
1 answer

How to run app.js in nodejs?

Hello @kartik, Assuming I have node and npm properly installed on the ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,880 points
2,494 views
0 votes
1 answer

How to get the available tasks list in gulp?

Hello @kartik,  I got it use the gulp --tasks in ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,880 points
887 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,880 points
8,354 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 781 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
2,280 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
3,414 views
0 votes
1 answer

How to shrink Navigation menu or bar on Scroll?

Hey, You can follow the steps below in ...READ MORE

answered Feb 19, 2020 in PHP by varun
3,453 views
0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,880 points
13,039 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,880 points
4,031 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP