Shraddha J
Shraddha J

Reputation: 784

How to accept input from user on command line in javascript?

I am writing some code in JavaScript and running it using the command line using the command node filename.js. I want the user to enter input values on the command line and use these values for further process and output.

Upvotes: 0

Views: 2573

Answers (1)

nico
nico

Reputation: 196

Use prompts npm package

npm install --save prompts

const prompts = require('prompts');

const questions = [
  {
    type: 'text',
    name: 'dish',
    message: 'Do you like pizza?'
  },
  {
    type: prev => prev == 'pizza' ? 'text' : null,
    name: 'topping',
    message: 'Name a topping'
  }
];

(async () => {
  const response = await prompts(questions);
})();

Upvotes: 1

Related Questions