Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using node.js's prompt for user input?

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;
    }
};
like image 827
Dat8StringGuy Avatar asked Dec 05 '16 18:12

Dat8StringGuy


People also ask

How to get user input from command line in Node JS?

Use Node.js Prompt module to get user input from command line example code. // Include prompt module. // This json object is used to configure what data will be retrieved from command line. // The fist input text is assigned to username variable.

How to prompt in Node JS?

One way is to use Node.js build-in process.stdin object or readline module, the other way is to use a Node.js third-party prompt module. 1.

How to take the input from the user synchronously in Node JS?

readline-sync: This is the third party module that is used for taking the input from the user synchronously. So, the execution of the programs is line by line. Filename- index.js: Taking input array from the user Run index.js file using below command: Output: This will be in console output.

How do I get the input character encoding in Node JS?

Use Node.js Process Object’s Stdin. // Get process.stdin as the standard input object. // 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. // User input exit.


2 Answers

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?

like image 96
Lemons Avatar answered Oct 02 '22 19:10

Lemons


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.

like image 37
Paul Avatar answered Oct 02 '22 19:10

Paul