Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
javascript
inquirer
command line
community creator

How to use the Inquirer Node package

Jon Deavers

Inquirer

Inquirer is an NPM package that provides an easy way to capture user input in your Node.js command line interface applications. It provides several methods for asking questions and returning answers from the user that can be accessed by a .then promise function.

Installation

As with all NPM packages, inquirer needs to be installed before you can use it. In the directory containing the node.js file you are working with, run npm i inquirer prior to writing an inquirer prompt.

Note: this will require you to have NPM and Node.js installed before beginning. Click the links to view their installation instructions.

Then, at the top of your file, you need to require inquirer as a dependency. This is accomplished with one line of code:

const inquirer = require("inquirer");

The require method lets your application know that it needs to use the inquirer package to execute the code below.

Example

Inquirer documentation can be found here and is very easy to follow. Let’s take a look at an example of inquirer in action:

For this example we’ll look at a simple question with basic typed user input.

1 inquirer.prompt([
2  {
3    name: 'greeting',
4    message: 'What would you like to say?',
5    type: 'input'
6   }])
7  .then(function(answer){
8    console.log(answer);
9  });

The expected output for this prompt is:

>> node index.js
>> What would you like to say? Hello World!
>> { greeting: 'Hello World!' 

Note: the string, “Hello World!” was typed into the command line interface when the application was run; thus, it provides user input data.

What’s going on here?

Inquirer was designed to return the answers to its input message in a call back function. You can see on line 7 that the answer parameter is passed into our .then promise, as mentioned above. Following this structure, you can use the answer parameter to perform actions inside the promise function.

Inquirer questions

As for the code inside the prompt (lines 2-6), these are a hash of values related to the question you want to ask the user. In its most basic form, an inquirer question requires a message (what you want to ask the user), a name (the key that will be accessed via answers.<name>), and a type (this can be one of 9 different input type options).

There’s a ton of versatility in those 9 options, so explore the docs and experiment with some other prompt types.

Sequential prompts

One of the great features of inquirer is the ability to chain several prompts into a single question. Let’s take a look at an expanded version of our example code above:

This example makes use of the list type in addition to the basic input type above.

1inquirer.prompt([
2  {
3    name: "greeting",
4    message: "What would you like to say?",
5    type: "input",
6  },
7  {
8    name: "colors",
9    message: "What's your favorite color?",
10   type: "list",
11   choices: ["black", "red", "blue", "yellow", "green", "whitesmoke"]
12 }])
13.then(function (answer) {
14 console.log(answer.greeting);
15 console.log(answer.colors);
16 });

The interface shown below displays the list type of the prompt in action.

>> node index.js
>> What would you like to say? Hello World!
>> What's your favorite color? (Use arrow key)
>> >black
>>  red
>>  blue
>>  yellow
>>  green
>>  whitesmoke

Explanation

This is the same block of code as above with two significant differences. First, we have a second question being chained onto the prompt in lines 7-12, and the inquirer.prompt method takes in an array of objects where each question object is asked in sequential order.

Lines 10 and 11 are a little different than our first example. Here, the answer to the question will be presented in a list that the user can scroll through and choose from the array in choices on line 11. When the user selects one of the choices provided, the answer.colors parameter will return whichever string the user selected from the list menu. This is extremely helpful when making a menu system with a basic switch statement.

The other significant difference is how the answer is accessed in our console.log functions on lines 14 and 15. Notice how in the first example, we logged the complete answer, and the output returned was in object format. By referencing the name values, as defined in our inquirer prompts, the console will only log the user’s input for that particular prompt.

Summary

Inquirer can be a powerful tool for interacting with your user in Node.js applications. There are many other features offered by inquirer including input data validation, user interface and layout options, password masking, and more. Additionally, the community has developed a collection of useful plugins.

So dive in and build something helpful today!

RELATED TAGS

node.js
javascript
inquirer
command line
community creator
RELATED COURSES

View all Courses

Keep Exploring