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;
}
};
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.
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.
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With