Now that we've chosen Inquirer as the library to use in our project, we need to use it! To do so the current questions in the CLI will be swapped out with inquirer prompt
API. You'll see some immediate benefits in styling and functionality that we get for free now that inquirer is being used.
Instructor: [0:00] At the moment, I'm using my readline promises to ask this question that asks the two questions so I can run node index.js. I wrote node index.js that asks you my name and where I live. Brilliant. [0:12] I want to refactor this to use the inquirer library. The first thing I'll do is I'll install npm install inquirer, and then I'm going to replace this import with import inquirer from inquirer.
[0:24] You don't have to create the interface anymore. I do have my two questions, question one and question two. I've run console.log and these two answers.
[0:32] Let's get our answers. Our answer is going to be returned after we await for inquirer.prompt. Now, inquirer.prompt takes an array of questions.
[0:42] Each question is represented by an object. The fields we have to include here are the type of question, the name of the question, so that it'll be referenced when it's returned and the message like what question we want to ask.
[0:55] What is your name was the first question. What type is that going to be? That's going to be of type input. You can see there are lots to choose.
[1:02] There's checkboxes, confirms, expands, inputs, lists, passwords, and there are other things that we can have as well. For now, it's going to be input and its named, let's call it name. That's our first question.
[1:13] Our second question then is going to be live. That is this question, "Where do you live?" This answers is going to be an object. I want to get answers.name for name and answers.live for live. The rl is closed, I don't need anymore.
[1:28] Let me try that, node index.js "What's your name?" It looks prettier. I've got this lovely question mark. I've got it in bold. If I type in my name, it goes in this lovely color. "Where do you live?" and "Your name is Kevin."
[1:42] This library gives me lots of other ways I can interact with the user. Instead of just an input, I could give a list, a list of choices. If I knew my tool was going to be targeting people in the UK, I could just say, NI for Northern Ireland, Wales, Scotland, England, and elsewhere.
[2:00] Now, I'll run this again. "What's your name?" "Kevin." "Where do you live?" Now, I actually have arrow keys. I've got my list, I can arrow up and down.
[2:09] That's just from changing, providing a type of list and some choices. This package allows me to have lots of really interesting ways to engage with my user that I may use and involve later going forward.
[2:22] The key is awaiting on Inquirer.prompt, which takes an array of questions, with type, name, and message, and other variables if it's a different type of question.
[2:32] Then, that returns an object, which has whatever the user returned keyed to whatever we named them inside of the question definition.