the complete solution for node.js command-line programs
Commander is the de facto standard library for building command-line interfaces in Node.js, with over 275 million weekly downloads. It provides a declarative API for defining commands, options, and arguments while automatically handling parsing, validation, and help generation. The library has zero dependencies and powers major developer tools including webpack-cli, babel-cli, and countless other CLI applications in the npm ecosystem.
The package solves the tedious problem of manually parsing process.argv and implementing consistent option handling across CLI tools. Instead of writing custom argument parsing logic with brittle string manipulation, Commander lets you declare your CLI structure and handles the rest. It distinguishes between options (flags like --verbose or --port 3000) and positional arguments, supports subcommands for complex tools, and generates professional help output automatically.
Commander is designed for developers building anything from simple scripts to complex multi-command CLIs. Its API strikes a balance between simplicity for basic use cases and power for advanced scenarios like custom option processing, variadic arguments, and git-style subcommands. The library has been battle-tested since 2011 and maintains backward compatibility while adding modern features.
#!/usr/bin/env node
import { Command } from 'commander';
import { readFileSync } from 'fs';
const program = new Command();
program
.name('dbutil')
.description('Database utility for managing migrations')
.version('1.0.0');
program
.command('migrate')
.description('Run database migrations')
.option('-d, --direction <type>', 'migration direction', 'up')
.option('-s, --steps <number>', 'number of steps', parseInt, 1)
.option('--dry-run', 'preview changes without executing')
.argument('[environment]', 'target environment', 'development')
.action((environment, options) => {
console.log(`Migrating ${options.direction} in ${environment}`);
console.log(`Steps: ${options.steps}`);
if (options.dryRun) {
console.log('[DRY RUN] No changes will be made');
}
});
program
.command('seed')
.description('Seed database with initial data')
.requiredOption('-f, --file <path>', 'path to seed file')
.option('--clear', 'clear existing data first')
.action((options) => {
if (options.clear) {
console.log('Clearing existing data...');
}
const data = readFileSync(options.file, 'utf-8');
console.log(`Seeding from ${options.file} (${data.length} bytes)`);
});
program.parse();Build deployment scripts that accept environment flags, target servers, and configuration options. Commander parses inputs like deploy --env production --region us-east-1 api-service into structured objects your deployment logic can consume.
Create developer tooling with multiple subcommands, similar to git or npm. Define commands like tool init, tool build --watch, and tool deploy --dry-run where each subcommand has its own options and arguments.
Write database migration tools that handle commands like migrate up, migrate down --steps 3, or migrate create <name> with proper argument validation and help documentation.
Implement code generators that accept templates, output directories, and configuration flags. Parse complex inputs like generate component Button --typescript --test --style=css with full validation.
Build system administration utilities that interact with servers, manage configurations, or automate DevOps tasks. Handle authentication options, verbose logging flags, and dry-run modes consistently across all commands.
npm install commanderpnpm add commanderbun add commander