0

I am using npm inquirer for the first time.

I am using a code similar to this:

const inquirer = require("inquirer");

const questions = [
  {
    type: "checkbox",
    name: "collections.telemetria",
    message: "Select collections of database telemetria",
    choices: [
      "chimera-11/14/2019,-4:22:38-PM",
      "chimera-11/14/2019,-4:28:26-PM"
    ]
  },
  {
    type: "checkbox",
    name: "collections.testa",
    message: "Select collections of database testa",
    choices: ["testa_c"]
  }
];

async function main() {
  const collections = (await inquirer.prompt(questions)).collections;
  console.log("collections:", collections);
  const outPath = await inquirer.prompt([
    {
      type: "input",
      name: "outPath",
      default: "./",
      message: "Insert the output path"
    }
  ]).outPath;
  console.log(outPath);
}
main();

The problem is that when it comes to the question of type input to be answered, the word undefined appears and I cannot put any input.

enter image description here

Here is a code sandbox: https://codesandbox.io/s/stoic-kowalevski-dgg5u

EuberDeveloper
  • 874
  • 1
  • 14
  • 38

2 Answers2

0

Issue is with the console.log(outPath) as that returns undefined making the terminal unusable. Remove console.log(outPath) and it should work.

I'm not really sure what .outPath at the end does but you can remove that to make console.log(outPath) to work.

So either remove console.log() or .outPath depending on requirement.

As you probalbly already know that inquirer.prompt returns a promise if you want the result you can do something like this

      const outPath = inquirer.prompt([
        {
          type: "input",
          name: "outPath",
          default: "./",
          message: "Insert the output path"
        }
      ]).then(response=>{
           console.log(response)
})

Here you can see it runs for me If I remove .outPath

Code Running

Shivam
  • 3,514
  • 2
  • 13
  • 27
0

Thanks to the advice of Shivam Sood, I found that there was only a mistake in the code. I forgot to put the await inquirer.prompt([...]) inside the parenthesis before calling the property outPath of the expected result.

So the right code should be:


const outPath = (await inquirer.prompt([
    {
      type: "input",
      name: "outPath",
      default: "./",
      message: "Insert the output path"
    }
  ])).outPath;
  console.log(outPath);

EuberDeveloper
  • 874
  • 1
  • 14
  • 38