DEV Community

Alex Spinov
Alex Spinov

Posted on

Inquirer.js Has a Free API — Here's How to Build Interactive CLI Prompts

Inquirer.js is a collection of interactive CLI prompts. It handles text input, selections, confirmations, checkboxes, and password inputs — making your CLI tools feel professional.

Installation

npm install @inquirer/prompts
Enter fullscreen mode Exit fullscreen mode

Basic Prompts

import { input, select, confirm, checkbox, password } from "@inquirer/prompts";

// Text input
const name = await input({ message: "What is your name?" });

// Select one option
const framework = await select({
  message: "Choose a framework:",
  choices: [
    { name: "React", value: "react" },
    { name: "Vue", value: "vue" },
    { name: "Svelte", value: "svelte" },
    { name: "Angular", value: "angular" }
  ]
});

// Confirm
const proceed = await confirm({ message: "Continue?", default: true });

// Multiple selection
const features = await checkbox({
  message: "Select features:",
  choices: [
    { name: "TypeScript", value: "ts", checked: true },
    { name: "ESLint", value: "eslint" },
    { name: "Prettier", value: "prettier" },
    { name: "Testing", value: "test" }
  ]
});

// Password
const token = await password({ message: "Enter API token:", mask: "*" });

console.log({ name, framework, proceed, features, token });
Enter fullscreen mode Exit fullscreen mode

Validation

const email = await input({
  message: "Email:",
  validate: (value) => {
    if (!value.includes("@")) return "Please enter a valid email";
    return true;
  }
});

const port = await input({
  message: "Port:",
  default: "3000",
  validate: (value) => {
    const num = parseInt(value);
    if (isNaN(num) || num < 1 || num > 65535) return "Port must be 1-65535";
    return true;
  }
});
Enter fullscreen mode Exit fullscreen mode

Project Scaffolding Example

async function scaffold() {
  const config = {
    name: await input({ message: "Project name:" }),
    template: await select({
      message: "Template:",
      choices: [
        { name: "Express API", value: "express" },
        { name: "Next.js App", value: "nextjs" },
        { name: "CLI Tool", value: "cli" }
      ]
    }),
    features: await checkbox({
      message: "Features:",
      choices: [
        { name: "TypeScript", value: "ts" },
        { name: "Docker", value: "docker" },
        { name: "CI/CD", value: "ci" }
      ]
    }),
    git: await confirm({ message: "Initialize git?" })
  };

  console.log("Creating project with:", config);
}

await scaffold();
Enter fullscreen mode Exit fullscreen mode

Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.

Top comments (0)