Wondering what’s next for npm?Check out our public roadmap! »

    minimal-password-prompt

    1.1.0 • Public • Published

    minimal-password-prompt

    npm bundle size NPM GitHub top language GitHub issues

    Minimal hidden password prompt for Node.js! [npm] [github]

    prompt-password example

    Simple, no extra hassles

    package dependencies dependents total lines of production code
    minimal-password-prompt 0 0 48
    password-prompt 2 43 2147

    Seriously, you can also just copy and paste to your project:

    const stdin = process.stdin;
    const stdout = process.stdout;
    
    const captureStdin = (onData) => {
      stdin.setEncoding('utf-8');
      stdin.setRawMode(true);
      stdin.on('data', onData);
      stdin.resume();
    };
    
    const releaseStdin = (onData) => {
      stdin.pause();
      stdin.removeListener('data', onData);
      stdin.setRawMode(false);
      stdout.write('\n');
    };
    
    const prompt = (question, ctrlcExits = true) => (
      new Promise((resolve, reject) => {
        let input = '';
        const onData = (data) => {
          switch (data) {
            case '\u000A': // \n
            case '\u000D': // \r
            case '\u0004': // Ctrl+D
              releaseStdin(onData);
              resolve(input);
              break;
            case '\u0003': // Ctrl+C
              releaseStdin(onData);
              ctrlcExits // exit or raise error
                ? process.exit()
                : reject(new Error('Ctrl+C'));
              break;
            case '\u0008': // Backspace
            case '\u007F': // Delete
              input = input.slice(0, -1);
              break;
            default: // any other
              input += data;
          };
        };
        captureStdin(onData);
        stdout.write(question);
      })
    );
    
    module.exports = prompt;

    (please sill do remember to include MIT license/copyright)

    Usage

    const prompt = require('minimal-password-prompt');
    
    prompt('enter password: ').then(pw => {
      console.log(`Password: ${pw}`);
    });

    or with async/await:

    const prompt = require('minimal-password-prompt');
    
    (async () => {
      const pw = await prompt('enter password: ');
      console.log(`password: ${pw}`);
    })();

    More examples here

    prompt reference

    Parameters:

    • question (String) prompt output before user input
    • ctrlcExits (Boolean), optional
      • true: Ctrl+C exits program (default)
      • false: Ctrl+C throws error

    Returns:

    • Promise<String> user input during prompt

    Installation

    Install with npm:

    npm install --save minimal-password-prompt
    

    Install

    npm i minimal-password-prompt

    DownloadsWeekly Downloads

    3

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    7.58 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar