0

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.

6
  • nodejs.org/en/knowledge/command-line/… Commented Sep 23, 2019 at 10:37
  • nodejs.dev/accept-input-from-the-command-line-in-nodejs Commented Sep 23, 2019 at 10:38
  • you want the user input as a parameter to script i.e. node filename.js --p1 param1 --p2 param2 or in the console. i.e. user runs the script the asked for the first param then second and so on? Commented Sep 23, 2019 at 10:44
  • @AZ_ I want input in the console and want to accept multiple values one by one Commented Sep 23, 2019 at 11:05
  • then you can use readline module as suggested by @CodeF0x Commented Sep 23, 2019 at 11:06

1 Answer 1

1

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);
})();
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting error as below: internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module 'prompts'
cd into your project folder & run npm install --save prompts then try again
Yes it did worked. But how can I get values entered by the user?
Thanks. I got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.