A right way to query password from a console in JavaScript

import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
import { Writable } from 'node:stream';

var mutableStdout = new Writable({
  write: function(chunk, encoding, callback) {
    if (!this.muted)
      output.write(chunk, encoding);
    callback();
  }
});

mutableStdout.muted = false;

var rl = readline.createInterface({
  input: input,
  output: mutableStdout,
  terminal: true
});
const amount = await rl.question('Amount: ');

output.write('Secret: ')

mutableStdout.muted = true;

const secret = await rl.question();

output.write('\n')

console.log(`You entered: ${amount}, ${secret}`);

rl.close();

This requires type to be module in package.json:

{
  "type": "module",
  "name": "xrp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ripple-lib": "^1.2.4"
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *