Programming

Building CLI Tools with Node.js

12 ديسمبر 202510 min read
Building CLI Tools with Node.js

Create powerful command-line tools using Node.js.

Why Build CLI Tools?

CLI tools automate repetitive tasks. Build custom utilities for your projects or share them with the community.

Project Setup

// package.json
{
  "name": "my-cli",
  "version": "1.0.0",
  "bin": {
    "my-cli": "./index.js"
  }
}

Main File

#!/usr/bin/env node
const { program } = require('commander');

program
  .name('my-cli')
  .version('1.0.0')
  .description('My custom CLI tool');

program
  .command('greet ')
  .description('Greet someone')
  .option('-l, --loud', 'Say it loudly')
  .action((name, options) => {
    const greeting = `Hello ${name}!`;
    console.log(options.loud ? greeting.toUpperCase() : greeting);
  });

program.parse();

Adding Colors and Prompts

const chalk = require('chalk');
const inquirer = require('inquirer');

console.log(chalk.green.bold('Success!'));
console.log(chalk.red('Error!'));

const answers = await inquirer.prompt([
  { type: 'input', name: 'name', message: 'Your name?' },
  { type: 'confirm', name: 'sure', message: 'Are you sure?' }
]);

Conclusion

Commander for commands, chalk for colors, inquirer for interactivity—a powerful combination for CLI tools.

Tags

#Node.js#CLI#Command Line#npm#Tools

Related Posts