🌐 Detecting your location…

কিভাবে 2026 সালে Node.js দিয়ে একটি CLI টুল তৈরি করবেন: সম্পূর্ণ নির্দেশিকা

⏱️4 min read  ·  668 words

কমান্ড-লাইন সরঞ্জামগুলি পুনরাবৃত্তিমূলক কাজ, ভারা প্রকল্প এবং পাওয়ার ডেভেলপার ওয়ার্কফ্লোগুলিকে স্বয়ংক্রিয় করে। Node.js CLI – পরিচিত জাভাস্ক্রিপ্ট, একটি সমৃদ্ধ ইকোসিস্টেম এবং সহজ npm বিতরণের জন্য চমৎকার। এই নির্দেশিকা স্ক্র্যাচ থেকে একটি সম্পূর্ণ, প্রকাশযোগ্য CLI টুল তৈরি করে।

কেন CLI টুল তৈরি করবেন?

  • স্বয়ংক্রিয় কর্মপ্রবাহ: পুনরাবৃত্তিমূলক কাজগুলিকে একটি একক আদেশে পরিণত করুন
  • সহজে বিতরণ করুন: npm-এ প্রকাশ করুন, একটি কমান্ড দিয়ে বিশ্বব্যাপী ইনস্টল করুন
  • পরিচিত ভাষা: একটি বিশাল ইকোসিস্টেম সহ জাভাস্ক্রিপ্ট/টাইপস্ক্রিপ্ট
  • ক্রস-প্ল্যাটফর্ম: Node.js যে কোন জায়গায় চলে

প্রকল্প সেটআপ

mkdir my-cli && cd my-cli
npm init -y
npm install commander chalk inquirer ora
// package.json - add the bin field and type
{
  "name": "my-cli",
  "version": "1.0.0",
  "type": "module",
  "bin": {
    "mycli": "./index.js"
  }
}

কমান্ডারের সাথে বেসিক CLI

#!/usr/bin/env node
// index.js - the shebang line makes it executable
import { program } from 'commander';

program
  .name('mycli')
  .description('A helpful developer CLI')
  .version('1.0.0');

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

program.parse();
# Test it locally
node index.js greet Alice
# Hello, Alice!
node index.js greet Alice --loud
# HELLO, ALICE!

চক দিয়ে রঙিন আউটপুট

import chalk from 'chalk';

console.log(chalk.green('✓ Success!'));
console.log(chalk.red('✗ Error occurred'));
console.log(chalk.yellow('⚠ Warning'));
console.log(chalk.blue.bold('Info:'), 'processing...');

// Combine styles
console.log(chalk.bgBlue.white(' TITLE '));
console.log(chalk.dim('subtle secondary text'));

অনুসন্ধানকারীর সাথে ইন্টারেক্টিভ প্রম্পট

import inquirer from 'inquirer';

async function setup() {
  const answers = await inquirer.prompt([
    {
      type: 'input',
      name: 'projectName',
      message: 'Project name?',
      default: 'my-app',
    },
    {
      type: 'list',
      name: 'framework',
      message: 'Choose a framework:',
      choices: ['React', 'Vue', 'Svelte'],
    },
    {
      type: 'confirm',
      name: 'typescript',
      message: 'Use TypeScript?',
      default: true,
    },
  ]);

  console.log(chalk.green(`Creating ${answers.projectName} with ${answers.framework}...`));
  return answers;
}

ওরা দিয়ে স্পিনার লোড হচ্ছে

import ora from 'ora';

async function installDeps() {
  const spinner = ora('Installing dependencies...').start();

  try {
    await runInstall();   // your async task
    spinner.succeed('Dependencies installed');
  } catch (err) {
    spinner.fail('Installation failed');
    throw err;
  }
}

একটি সম্পূর্ণ উদাহরণ: প্রজেক্ট স্ক্যাফোল্ডার

#!/usr/bin/env node
import { program } from 'commander';
import inquirer from 'inquirer';
import chalk from 'chalk';
import ora from 'ora';
import fs from 'fs/promises';

program
  .command('create')
  .description('Scaffold a new project')
  .action(async () => {
    const answers = await inquirer.prompt([
      { type: 'input', name: 'name', message: 'Project name?' },
      { type: 'list', name: 'template', message: 'Template?',
        choices: ['api', 'web', 'cli'] },
    ]);

    const spinner = ora('Creating project...').start();
    try {
      await fs.mkdir(answers.name, { recursive: true });
      await fs.writeFile(
        `${answers.name}/package.json`,
        JSON.stringify({ name: answers.name, version: '0.1.0' }, null, 2)
      );
      spinner.succeed(chalk.green(`Created ${answers.name}!`));
      console.log(chalk.dim(`\n  cd ${answers.name}\n  npm install\n`));
    } catch (err) {
      spinner.fail('Failed to create project');
      console.error(err);
      process.exit(1);
    }
  });

program.parse();

এনপিএমে পরীক্ষা এবং প্রকাশনা

# Link locally to test as a global command
npm link
mycli create        # test it works globally

# Unlink when done testing
npm unlink -g my-cli

# Publish to npm
npm login
npm publish

# Users install it globally
npm install -g my-cli
mycli --help

সর্বোত্তম অভ্যাস

  • একটি শেবাং যোগ করুন (#!/usr/bin/env node) তাই ফাইলটি এক্সিকিউটেবল হিসাবে চলে
  • সহায়ক — হেল্প আউটপুট প্রদান করুন — কমান্ডার আপনার বর্ণনা থেকে এটি তৈরি করেন
  • ত্রুটিগুলি সুন্দরভাবে পরিচালনা করুন — ব্যর্থতার ক্ষেত্রে নন-জিরো কোড সহ প্রস্থান করুন (process.exit(1))
  • স্পষ্ট প্রতিক্রিয়া দিন— স্পিনার, রঙ, এবং সাফল্য/ত্রুটির বার্তা || chainable API and is very popular. Yargs is powerful with more built-in features. For most CLIs, Commander is a great default. Try both and pick what feels natural.
  • Q: How do I make my CLI executable?A: Add a shebang line (
  • ) at the top of your entry file, and define the field in package.json mapping a command name to the file. After npm install -g (or npm link), the command is available globally.

Q: Should I use TypeScript for a CLI?

A: For larger CLIs, yes — type safety helps. Compile to JavaScript before publishing (or use a bundler like tsup). For small CLIs, plain JavaScript is simpler. TypeScript pays off as the tool grows.
Q: How do I handle configuration files?

A: Read config from a file (
, package.json field, or cosmiconfig which handles multiple formats). Let users configure defaults, then override with command-line flags. cosmiconfig is the standard library for this.#!/usr/bin/env nodeQ: Can I distribute a CLI without npm?binA: Yes — bundle it into a single executable with tools like pkg or Bun’s compile feature, producing a standalone binary users run without Node.js installed. Useful for wider distribution, though npm is simplest for developer audiences.

Conclusion
Building a CLI tool with Node.js is straightforward and rewarding. Use

Commander for argument parsing, Chalk for colored output, Inquirer for interactive prompts, এবং স্পিনার্সের জন্য
খুব.myclircখুব

খুব
খুব

খুব

খুবখুবখুবbinখুবnpm linkখুব

MD Rafikul Islam

Written by

MD Rafikul Islam is a software developer and the editor of TechPulse. He writes about developer tooling, hardware, and the practical decisions that come up in day-to-day engineering work — which laptop to buy, which framework to commit to, why a build broke at 2am. He tests the tools he writes about and says plainly when something is not worth the money. Corrections and corrections requests are welcome at rony.yf25@gmail.com.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা