Using node.js's prompt for user input?

15,787

Solution 1

Using require('prompt') or require('readline') are both good, easy ways to do this, but I wanted to find THE EASIEST WAY, and I didn't care about whether it is synchronous or asynchronous. Finding it took me longer than it should have, so maybe I can save people some time if they read this before digging any further.

Here you go:

# npm install readline-sync

var readline = require('readline-sync');

var name = readline.question("What is your name?");

console.log("Hi " + name + ", nice to meet you.");

Disclaimer: I am just spreading the word, here are my sources:

https://teamtreehouse.com/community/how-to-get-input-in-the-console-in-nodejs

How to take in text input from a keyboard and store it into a variable?

Solution 2

When you say "installed it from npm" I'm assuming you're referring to the prompt module from flatiron.

From their docs, as with most Node things, it looks like it exposes an asynchronous function, so you'll handle input inside the prompt callback:

var prompt = require('prompt');

  //
  // Start the prompt
  //
  prompt.start();

  //
  // Get two properties from the user: username and email
  //
  prompt.get(['username', 'email'], function (err, result) {
    //
    // Log the results.
    //
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
  });

Storing it in a variable would be no different than accessing it from the result object above, but realize that since it's async it'll only be reliable available inside that callback.

Share:
15,787
Dat8StringGuy
Author by

Dat8StringGuy

Jazz guitar graduate who found himself a love for all things logic after starting programming with MaxMSP. Currently doing my B.Sc. in Computer Science at Université de Montréal. Learning JS, Python, UNIX, Java and C++ (on my own). Check out my band: https://downtownutopia.bandcamp.com/releases

Updated on June 11, 2022

Comments

  • Dat8StringGuy
    Dat8StringGuy almost 2 years

    I'm working on a JS project running with node.js and I can't figure out how to get the prompt to work correctly for user input. I installed it from npm, followed the steps and I can get the program to prompt for user input but I can't store the result in a variable.

    What I want is to prompt the user for his next move (up,down,left or right) every turn and use the result. I tried making a global move variable and affecting it in the prompt section, but it doesn't seem to work.

      var Moving = function(maxMoves){
    
        for(var n=maxMoves; n>0;){
            var move;
            var l;
            //This would prompt for a direction (up,down,left,right)
            move = prompt();
            //And this would prompt for the number of tiles to advance;
            l = prompt();
            Direction(move,l);
            n-=l;
        }
    };
    
  • Dat8StringGuy
    Dat8StringGuy over 7 years
    I'm still a bit confused. Considering it's async, I'm not sure how to store it in a variable I can use immediately. I tried using that template and adding move = result;, but then when I tried to console.log(move), I got an undefined.
  • Paul
    Paul over 7 years
    As I said, the variables will only be reliably available inside the prompt callback. Again, this is an area where if you post your code we could more easily help you with specific advice.
  • Paul
    Paul over 7 years
    yes, and you're not following my answer at all. Once again. *this is asynchronous. The only place the prompt results will be is inside the callback. *