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.

// 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.");
        // 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

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 and get user input then display those data in console.
prompt.get(prompt_attributes, function (err, result) {
    if (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 =;
        var message = "  Username : " + username + " , Password : " + password + " , Email : " + email;

        // Display user input in console log.

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]

3 thoughts on “Node JS Get User Input From Command Line Prompt Example”

  1. I used your example 1 Use Node JS Process Object’s Stdin and tried to run this in Node and it will not exit no matter what I use in the If portion of the statment it will not exicute anything in the if block. It will only execute the else portion of the block. what is wrong with the code? I have tried to copy and paste just as it is. I have also tryed to change it nothing I do gets the if statment or what it in it to run?

Leave a Comment

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.