8

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
  • 301
  • 1
  • 2
  • 10
  • 3
    We cannot help you with code we cannot see. Reduce your problem to a [mcve], and put that in the question. – T.J. Crowder Dec 05 '16 at 18:03
  • Basically, independantly from my code, my question is simply: How to store a user input in a variable in node.js? – Dat8StringGuy Dec 05 '16 at 18:11
  • 1
    Duplicate of [*How can I take console input from a user in node.js?*](http://stackoverflow.com/questions/26683734/how-can-i-take-console-input-from-a-user-in-node-js) Please [search before posting.](/search?q=How+to+store+a+user+input+in+a+variable+in+node.js) More on searching [here](/help/searching). – T.J. Crowder Dec 05 '16 at 18:14

4 Answers4

11

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?

Imran
  • 5,542
  • 3
  • 23
  • 46
Lemons
  • 379
  • 4
  • 14
3

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • 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_. – Dat8StringGuy Dec 05 '16 at 18:20
  • 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 Dec 05 '16 at 18:20
  • 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. * – Paul Dec 05 '16 at 22:49
0

you can use two packages readline and prompt

Here's a simple example with readline package https://nodejs.org/en/knowledge/command-line/how-to-prompt-for-command-line-input/

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What is your name ? ', function (name) {
  rl.question('Where do you live ? ', function (country) {
    console.log(`${name}, is a citizen of ${country}`);
    rl.close();
  });
});

rl.on('close', function () {
  console.log('\nBYE BYE !!!');
  process.exit(0);
});

Here is an another example with prompt package. https://www.npmjs.com/package/prompt

const prompt = require('prompt');
prompt.start();
prompt.get(['username','email'], function (err, result) {
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
});
Syamlal
  • 714
  • 1
  • 11
  • 20
-1

inquirer module has a prompt function. Use

const { prompt } = require('inquirer');


prompt([array] | object | string ')