Node.js prompt.get() Method
The prompt.get() method is an asynchronous function. This method takes strings representing property names in addition to objects for complex property validation (and more). This function is used for I/O operations.
Syntax:
prompt.get([object]/[properties name], callbackfunction)
Parameters: This method takes two parameters as shown below:
- [object]/[properties name]: This parameter holds the property of the input value or predefined object.
- callbackfunction: This parameter holds the callback function to make this function an asynchronous function.
Installation Module:
-
You can visit the link to Install this module. You can install this package by using this command.
npm install prompt
-
After that, you can just create a folder and add a file for example, index.js. To run this file you need to run the following command.
node index.js
Filename: Index.js
var prompt = require( 'prompt' ); // Function call prompt.start(); // Reading two properties from user ie. name & class prompt.get([ 'Name' , 'class' ], function (err, result) { // Printing the result console.log( 'Command-line input received:' ); console.log( 'Name: ' + result.name); console.log( 'class: ' + result.class); }) |
Project Structure:
Run the index.js file using the following command:
node index.js
Output:
prompt: name: some-user prompt: class: 12th Command-line input received: name: some-user class: 12th