Node JS Get User Input From Command Line Prompt Example

This example will show you how to get user input data from command line in node js application. One way is to use node js build-in process stdin object, the other way is to use a node js third party prompt module.

1. Use Node JS Process Object’s Stdin.

// Get process.stdin as the standard input object.
var standard_input = process.stdin;

// Set input character encoding.
standard_input.setEncoding('utf-8');

// Prompt user to input data in console.
console.log("Please input text in command line.");

// When user input data and click enter key.
standard_input.on('data', function (data) {

    // User input exit.
    if(data === 'exit\n'){
        // Program exit.
        console.log("User input complete, program exit.");
        process.exit();
    }else
    {
        // Print user input in console.
        console.log('User Input Data : ' + data);
    }
});

Save above source code in a js file and execute the js in node like below.

$ node get-user-input-from-command-line-prompt.js
Please input text in command line.
hello node js
User Input Data : hello node js

i love node 
User Input Data : i love node

exit
User input complete, program exit.

2. Use Node JS Prompt Module.

Prompt is a third-party node module in npm repository. Before you can use it, you need to install the module into your project folder use below command. You can read How To Use Node Package Manager to learn more about node js module management example.

cd < Your node js current project directory >
npm install prompt

After install you can see a node_module folder is created in current project directory. Be sure you run above command in current project folder.

node module folder created in current project directory

Use node js Prompt module to get user input from command line example code.

// Include prompt module.
var prompt = require('prompt');

// This json object is used to configure what data will be retrieved from command line.
var prompt_attributes = [
    {
        // The fist input text is assigned to username variable.
        name: 'username',
        // The username must match below regular expression.
        validator: /^[a-zA-Z\s\-]+$/,
        // If username is not valid then prompt below message.
        warning: 'Username is not valid, it can only contains letters, spaces, or dashes'
    },
    {
        // The second input text is assigned to password variable.
        name: 'password',
        // Do not show password when user input.
        hidden: true
    },
    {
        // The third input text is assigned to email variable.
        name: 'email',
        // Display email address when user input.
        hidden: false
    }
];

// Start the prompt to read user input.
prompt.start();

// Prompt and get user input then display those data in console.
prompt.get(prompt_attributes, function (err, result) {
    if (err) {
        console.log(err);
        return 1;
    }else {
        console.log('Command-line received data:');

        // Get user input from result object.
        var username = result.username;
        var password = result.password;
        var email = result.email;
        var message = "  Username : " + username + " , Password : " + password + " , Email : " + email;

        // Display user input in console log.
        console.log(message);
    }
});

Below is the output when execute above code.

$ node get-user-input-from-command-line-prompt.js 

prompt: username:  jerry

prompt: password: 

prompt: email:  [email protected]

Command-line received data:

¬† Username : jerry , Password : 111111 , Email : [email protected]
(Visited 1,513 times, 24 visits today)
READ :   How To Run Node JS Server In Background

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.