简体   繁体   中英

In NodeJS, how can I get a password from input without the user's keypresses being echoed to the console?

I'm trying to add a simple setup prompt to my program, where it prompts for some basic details before starting the app for the first time. One of these inputs I need is a password. When I read this password, I want to ensure that (much like when you enter a password for sudo ) it doesn't appear in the user's terminal as they type.

How can I do so within NodeJS? I'm looking for something I can drop into the following code.

console.log('Failed to find config file');
// This echoes to the console - how can I do the same thing without the echo?
const password = prompt('Enter a password: ');

I'm happy to install a dependency from NPM if required.

Check this article

Console input operations are described here

Article utl

You can use the prompt-sync library to prompt for passwords.

import createPrompt from 'prompt-sync';
const prompt = createPrompt({});

// The prompt object's hide method gives a prompt where the input 
// is hidden
const password = prompt.hide('Enter a password: ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM