Open In App

Node.js prompt.get() Method

Last Updated : 15 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. [object]/[properties name]: This parameter holds the property of the input value or predefined object.
  2. callbackfunction: This parameter holds the callback function to make this function an asynchronous function.

Installation Module:

  1. You can visit the link to Install this module. You can install this package by using this command.

    npm install prompt
    
  2. 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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads