Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

How to set default value for an answer?

Open Cyclonecode opened this issue 7 years ago • 12 comments

I need to set the default answer even if when() succeeds from the beginning which in turn not will display the question for the user.

    {
        type: 'input',
        name: 'MYSQL_INCLUDE_DIR',
        message: 'Enter path to mysql headers',
        default: MYSQL_INCLUDE_DIR,
        when: (answers) => {
            return !fs.existsSync(MYSQL_INCLUDE_DIR)
        },
        validate: (path) => {
            return fs.existsSync(path)
        }
    }

    inquirer.prompt(questions)
     .then((answers) => {
       // answers.MYSQL_INCLUDE_DIR will be undefined if the default directory exists.
     })

This issue is related to the following SO question:

https://stackoverflow.com/q/52842442/1047662

Cyclonecode avatar Oct 16 '18 20:10 Cyclonecode