Building CLI Tools With Node.js

Automate your team's repetitive tasks with a CLI

Many of the tools developers use every day — npm, Vite, the Contentstack CLI, the Forge CLI — are Node.js CLIs. Building your own is a great way to automate team workflows.

Minimal CLI With Built-In parseArgs

#!/usr/bin/env node
import { parseArgs } from 'node:util';

const { values, positionals } = parseArgs({
  options: {
    env: { type: 'string', short: 'e', default: 'staging' },
    verbose: { type: 'boolean', short: 'v' },
  },
  allowPositionals: true,
});

console.log(`Deploying ${positionals[0]} to ${values.env}`);

Larger CLIs With Commander

import { Command } from 'commander';
const program = new Command();

program.name('jira-tools').version('1.0.0');

program.command('report')
  .option('-p, --project <key>', 'project key')
  .action(async ({ project }) => {
    const issues = await fetchOpenIssues(project);
    console.table(issues.map((i) => ({ key: i.key, status: i.status })));
  });

program.parse();

Interactive Prompts

Libraries like @inquirer/prompts or @clack/prompts provide selects, confirms, and text inputs.

Output

  • Use colours sparingly (node:util styleText or picocolors)
  • Support --json output for scripting
  • Exit with non-zero codes on failure

Packaging

{
  "name": "@myteam/jira-tools",
  "bin": { "jira-tools": "./bin/cli.js" },
  "type": "module"
}

Then npm link for local testing or publish to npm (or a private registry).

Good CLI Manners

  • --help for every command
  • Clear error messages with next steps
  • Don't prompt when not in an interactive terminal

If your team repeats the same five commands every day, wrap them in a CLI.

Arivanandhan Chitheshwaran