Guide
Build a CLI with Node.js: Commander vs yargs vs 2026



Build production-ready Node.js CLI tools in 2026. Commander, yargs, and oclif compared with real code examples, TypeScript setup, and publishing to npm.
Contents
Contents
TL;DR
Use Commander for a CLI with explicit commands and options, yargs when commands share validation or configuration, and oclif when you need a plugin system. Commander pairs with tsx for local development and esbuild for a bundled script you can publish to npm.
Key Takeaways
- Commander chains command definitions, flags, and action handlers.
- yargs provides command builders, argument validation, and middleware.
- oclif can discover command classes from a project directory and load plugins.
- esbuild can bundle a CLI into a JavaScript file. Publish it with a shebang and point the package's
binentry at that file.
Why CLI Framework Choice Matters
Before the JavaScript ecosystem matured, most Node.js CLI tools were built with raw process.argv parsing or the built-in readline module. That works for simple cases but quickly becomes unmaintainable as you add subcommands, option validation, help text generation, and shell completion.
Modern CLI frameworks handle all of that scaffolding so you can focus on what your tool actually does. The choice of framework shapes how your project scales: a single-purpose build tool has very different requirements from a developer platform CLI with dozens of subcommands and a plugin ecosystem.
The deciding factor is where you want the framework to take over. Commander lets you register commands directly. yargs can apply shared parsing and validation rules to several commands. oclif gives independently maintained commands and plugins a common project structure.
Commander.js: The Minimalist Standard
Register a subcommand with .command(), declare its flags with .option(), and connect it to a handler with .action(). A nested command group can use the same pattern.
#!/usr/bin/env node
// src/cli.ts — Commander-based CLI
import { Command } from 'commander';
import { readFileSync } from 'fs';
import { resolve } from 'path';
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
const program = new Command();
program
.name('mytool')
.description('My awesome CLI tool')
.version(pkg.version);
// Simple command with positional argument and options
program
.command('hello <name>')
.description('Say hello to someone')
.option('-l, --loud', 'Loud mode (uppercase)')
.option('-t, --times <number>', 'How many times', '1')
.action((name: string, options: { loud: boolean; times: string }) => {
const times = parseInt(options.times);
const message = options.loud ? `HELLO, ${name.toUpperCase()}!` : `Hello, ${name}!`;
for (let i = 0; i < times; i++) {
console.log(message);
}
});
// Subcommand group — build and deploy under one namespace
const deploy = program.command('deploy').description('Deployment operations');
deploy
.command('build')
.description('Build the project for deployment')
.option('--env <env>', 'Target environment', 'production')
.option('--no-cache', 'Skip build cache')
.action(async (options: { env: string; cache: boolean }) => {
console.log(`Building for ${options.env}${options.cache ? '' : ' (no cache)'}...`);
// Build logic here
});
deploy
.command('push <target>')
.description('Push build artifacts to target')
.option('--tag <tag>', 'Image tag to push', 'latest')
.option('--dry-run', 'Print what would happen without executing')
.action(async (target: string, options: { tag: string; dryRun: boolean }) => {
if (options.dryRun) {
console.log(`Would push tag ${options.tag} to ${target}`);
return;
}
console.log(`Pushing ${options.tag} to ${target}...`);
});
// Global error handler — cleaner output than default
program.configureOutput({
writeErr: (str) => process.stderr.write(`Error: ${str}`),
outputError: (str, write) => write(`\n${str}\n`),
});
// exitOverride makes Commander throw instead of calling process.exit
// This makes your CLI testable without process mocking
program.exitOverride();
program.parse();
Commander's exitOverride() is worth noting: by default, Commander calls process.exit(1) on errors. Calling exitOverride() makes it throw a CommanderError instead, which is essential for unit testing your CLI without mocking process.exit.
The program.command('deploy') pattern creates a command group. Subcommands (build, push) are added to deploy rather than program, giving you mytool deploy build and mytool deploy push — clean namespacing without any special configuration.
yargs: Middleware and Rich Validation
In a yargs command builder, declare option types and valid values next to the handler that consumes them. Add a .check() for dependencies between flags and middleware for work shared across commands.
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { readFileSync } from 'node:fs';
const config = process.env.CONFIG_PATH
? JSON.parse(readFileSync(process.env.CONFIG_PATH, 'utf8'))
: {};
yargs(hideBin(process.argv))
.scriptName('mytool')
.usage('$0 <command> [options]')
.config(config)
.command(
'build',
'Build the project',
(yargs) =>
yargs
.option('env', {
type: 'string',
description: 'Target environment',
choices: ['development', 'staging', 'production'] as const,
default: 'production',
})
.option('cache', {
type: 'boolean',
default: true,
description: 'Use the build cache (disable with --no-cache)',
}),
async (argv) => {
console.log(`Building for ${argv.env}${argv.cache ? '' : ' (no cache)'}...`);
}
)
.command(
'deploy <target>',
'Deploy to an environment',
(yargs) =>
yargs
.positional('target', {
describe: 'Deployment target',
choices: ['staging', 'production'] as const,
})
.option('tag', {
type: 'string',
description: 'Docker image tag',
default: 'latest',
})
.option('dry-run', {
type: 'boolean',
default: false,
})
// yargs .check() — custom validation with helpful error messages
.check((argv) => {
if (argv.target === 'production' && argv.tag === 'latest') {
throw new Error('Deploying "latest" to production is not allowed — use a specific tag');
}
return true;
}),
async (argv) => {
const { target, tag, dryRun } = argv;
console.log(`Deploying ${tag} to ${target}${dryRun ? ' (dry run)' : ''}`);
}
)
// Global middleware — runs before every command handler
.middleware((argv) => {
if (argv.verbose) {
console.log('[verbose] Running command:', argv._[0]);
}
})
.option('verbose', {
type: 'boolean',
alias: 'v',
global: true,
description: 'Enable verbose logging',
})
.help()
.strict() // Error on unknown options
.parse();
The .check() function is one of yargs's most useful features. It runs after argument parsing but before your command handler, with access to all parsed values. This is where you validate cross-option dependencies — "if deploying to production, require a non-latest tag" — things that can't be expressed with simple type or choice constraints.
Load configuration with .config() so those values enter parsing and validation. With strict parsing, use config keys accepted by the command you run. The middleware here logs the command name when verbose mode is enabled.
yargs also ships with built-in shell completion via .completion(), which generates a completion script you can source in your shell. Commander requires a separate package like omelette for the same functionality.
oclif: Enterprise Multi-Command Architecture
With oclif's file-based discovery, a command class lives in its own module. Scaffold the project to get the command layout, then use plugins to extend a CLI without editing its core commands.
Heroku and Salesforce built CLIs with oclif. Its file-based layout helps when separate teams own different command groups or ship plugins independently.
# Scaffold an oclif CLI project
npx oclif generate mycli
cd mycli
# Project structure:
# src/commands/ ← Each file = one command
# src/commands/build.ts ← mytool build
# src/commands/deploy.ts ← mytool deploy
# src/commands/deploy/ ← Subcommand directory
# src/hooks/ ← Lifecycle hooks (prerun, postrun, init)
# src/base-command.ts ← Optional base class for shared behavior
// src/commands/build.ts — oclif command as a class
import { Command, Flags } from '@oclif/core';
export default class Build extends Command {
static description = 'Build the project for deployment';
static examples = [
'<%= config.bin %> build',
'<%= config.bin %> build --env staging --no-cache',
];
static flags = {
env: Flags.string({
char: 'e',
description: 'Target environment',
options: ['development', 'staging', 'production'],
default: 'production',
}),
'no-cache': Flags.boolean({
description: 'Skip build cache',
default: false,
}),
};
async run() {
const { flags } = await this.parse(Build);
this.log(`Building for ${flags.env}...`);
if (flags['no-cache']) {
this.warn('Cache disabled — build will be slower');
}
// Build logic here
this.log('Build complete');
}
}
// src/commands/deploy.ts — oclif deploy command with args
import { Command, Flags, Args } from '@oclif/core';
export default class Deploy extends Command {
static description = 'Deploy to an environment';
static args = {
target: Args.string({
description: 'Deployment target',
required: true,
options: ['staging', 'production'],
}),
};
static flags = {
tag: Flags.string({
char: 't',
description: 'Docker image tag',
default: 'latest',
}),
'dry-run': Flags.boolean({
description: 'Simulate deployment without executing',
default: false,
}),
};
async run() {
const { args, flags } = await this.parse(Deploy);
if (args.target === 'production' && flags.tag === 'latest') {
this.error('Cannot deploy "latest" to production — specify a tag with --tag', { exit: 1 });
}
this.log(`Deploying ${flags.tag} to ${args.target}...`);
if (flags['dry-run']) {
this.warn('Dry run — no changes will be made');
return;
}
this.log('Deployment complete');
}
}
oclif's plugin architecture is its defining feature for large CLIs. Plugins are npm packages that export additional commands. Users can install them with mycli plugins install @mycli/analytics, and the commands appear in mycli automatically — no code changes required. This is exactly how the Salesforce CLI ships hundreds of commands across dozens of packages.
TypeScript and Build Setup
All three frameworks work well with TypeScript. The recommended setup differs:
{
"name": "mycli",
"version": "1.0.0",
"type": "module",
"bin": {
"mycli": "./dist/cli.js"
},
"scripts": {
"dev": "tsx src/cli.ts",
"build": "esbuild src/cli.ts --bundle --platform=node --target=node20 --outfile=dist/cli.js --banner:js='#!/usr/bin/env node'",
"prepublishOnly": "npm run build"
},
"dependencies": {
"commander": "^12.0.0"
},
"devDependencies": {
"esbuild": "^0.23.0",
"tsx": "^4.0.0",
"typescript": "^5.3.0",
"@types/node": "^20.0.0"
}
}
The --banner:js='#!/usr/bin/env node' flag in the esbuild command adds the shebang line to the output — esbuild doesn't do this automatically. Without it, users would need to invoke your CLI with node mycli rather than just mycli.
The tsx devDependency enables npm run dev which runs TypeScript directly without a compile step, dramatically speeding up the development loop. You only run esbuild when you need to publish.
For oclif, the scaffolded project uses tsc directly rather than esbuild, since oclif's plugin loading system depends on the file structure that TypeScript compilation preserves.
Distribution: npm, npx, and Standalone Binaries
# Development: test locally before publishing
npm link # Creates a global symlink to your current build
mycli --help # Works from anywhere on your machine
npm unlink mycli # Remove when done
# Publishing to npm
npm publish --access public # Public package
npm publish --access restricted # Private (requires paid account)
# For scoped packages: @yourname/mycli
# npm publish --access public for scoped
# Users install globally
npm install -g mycli
# or run without installing
npx mycli --help
For CLIs that need to run on machines without Node.js installed (enterprise tools, DevOps utilities), you can bundle into standalone binaries using pkg or caxa:
# pkg — creates binaries for macOS, Linux, Windows
npm install -g pkg
pkg dist/cli.js --targets node20-macos-arm64,node20-linux-x64,node20-win-x64 --output dist/mycli
# Result:
# dist/mycli-macos-arm64 (~80MB — includes Node.js runtime)
# dist/mycli-linux-x64
# dist/mycli-win-x64.exe
The size (~80MB) is the tradeoff — you're bundling the entire Node.js runtime. For most developer tools, that's acceptable if it means zero-dependency installation for end users.
Shell Completion
Shell completion is an often-overlooked but significant quality-of-life feature. Users who can tab-complete your commands and flags adopt tools faster.
yargs has the cleanest built-in solution:
yargs(hideBin(process.argv))
// ... commands ...
.completion('completion', 'Generate shell completion script')
.parse();
Users install it once:
mycli completion >> ~/.zshrc && source ~/.zshrc
# Now: mycli de<TAB> → mycli deploy
Commander requires a third-party package like omelette:
import omelette from 'omelette';
const completion = omelette('mycli <command>');
completion.on('command', ({ reply }) => {
reply(['build', 'deploy', 'config']);
});
completion.init();
oclif generates completion scripts automatically via @oclif/plugin-autocomplete — one of its official plugins. Add the plugin to your package.json and users get mycli autocomplete setup to install completions for bash, zsh, or fish.
Package Health
| Package | How you organize commands | What to check before adopting |
|---|---|---|
commander | Register commands and nested commands on a program | Whether explicit registration is easy to maintain |
yargs | Define commands with builders, checks, and middleware | Whether configuration and validation rules are shared across commands |
@oclif/core | Discover command classes and load plugins | Whether you need a plugin lifecycle and separate command modules |
When to Choose Each
| Scenario | Best Pick |
|---|---|
| Utility with a small, fixed command set | Commander |
| Script with complex option validation | yargs |
| Need middleware / config file loading | yargs |
| Built-in shell completion | yargs |
| Command groups owned by different teams | oclif |
| Plugin architecture for end-user extensibility | oclif |
| CLI that ships as a platform (like Heroku CLI) | oclif |
| Run TypeScript during development and publish a bundled script | Commander + tsx + esbuild |
| Migrating from Java/Spring CLI patterns | oclif (class-based) |
Start with Commander if you can describe the whole CLI as a tree of commands and flags. Reach for yargs when you need a common validation or configuration layer. Use oclif when contributors need a standard way to ship commands and plugins.
Testing CLI Applications
Testing is the area where CLI development differs most from library or application development, and it is frequently skipped because the patterns are less obvious. A CLI has three testable surfaces: the argument parsing logic, the command handler behavior, and the program's stdout/stderr output. Each requires a different approach.
Commander's exitOverride() is the foundation for unit testing argument parsing. Without it, any Commander error calls process.exit(1), which terminates your test process. With exitOverride(), Commander throws a CommanderError that your tests can catch:
// test/cli.test.ts
import { describe, it, expect } from 'vitest';
import { program } from '../src/cli';
describe('CLI argument parsing', () => {
it('rejects unknown options', () => {
expect(() => program.parse(['node', 'mycli', '--unknown-flag'])).toThrow();
});
it('parses --times as integer', () => {
let capturedOptions: { times: string } | undefined;
program.command('hello <name>').action((name, options) => {
capturedOptions = options;
});
program.parse(['node', 'mycli', 'hello', 'Alice', '--times', '3']);
expect(capturedOptions?.times).toBe('3');
});
});
For testing stdout and stderr output, capture the streams rather than mocking console.log. The standard pattern is to replace process.stdout.write and process.stderr.write temporarily for each test:
function captureOutput(fn: () => void): { stdout: string; stderr: string } {
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
const originalStdout = process.stdout.write.bind(process.stdout);
const originalStderr = process.stderr.write.bind(process.stderr);
process.stdout.write = (chunk: string) => { stdoutChunks.push(chunk); return true; };
process.stderr.write = (chunk: string) => { stderrChunks.push(chunk); return true; };
try {
fn();
} finally {
process.stdout.write = originalStdout;
process.stderr.write = originalStderr;
}
return { stdout: stdoutChunks.join(''), stderr: stderrChunks.join('') };
}
Integration testing — running your actual compiled binary and checking what it produces — is the most realistic test level for CLIs. Use Node.js's child_process.execSync or execFileSync for synchronous CLIs, and execFile with a callback for async behavior:
import { execFileSync } from 'child_process';
import { resolve } from 'path';
const CLI_PATH = resolve(__dirname, '../dist/cli.js');
describe('CLI integration', () => {
it('outputs help text when called with --help', () => {
const result = execFileSync('node', [CLI_PATH, '--help'], {
encoding: 'utf8',
env: { ...process.env, NODE_ENV: 'test' },
});
expect(result).toContain('Usage:');
expect(result).toContain('Commands:');
});
it('exits with code 1 on invalid arguments', () => {
expect(() =>
execFileSync('node', [CLI_PATH, '--invalid-flag'], { encoding: 'utf8' })
).toThrow(/Command failed/);
});
});
Snapshot testing is valuable for CLI output that changes infrequently but needs to remain consistent. Testing that your --help output looks exactly right across versions catches accidental formatting regressions. Vitest's toMatchSnapshot() works well for this: the first run creates the snapshot, subsequent runs compare against it. Update snapshots deliberately with vitest --update-snapshots when you intentionally change output formatting.
Distribution and Publishing Strategy
Getting your CLI from a local build to something users can install requires decisions that affect user experience significantly.
The bin field in package.json is the entry point for npm-distributed CLIs. When a user runs npm install -g mycli, npm reads the bin field and creates a symlink from a directory on the user's PATH to your CLI's entry file. The entry file must have the #!/usr/bin/env node shebang as its first line — without it, the file is not executable as a command.
Publishing to npm with npm publish makes your CLI installable with both npm install -g mycli (global persistent install) and npx mycli (ephemeral run). npx downloads and runs the package in a temporary directory, which is increasingly the preferred distribution model because users do not need to manage globally installed packages. For CLIs that users run infrequently or just to bootstrap a project, npx is the expected install-free invocation pattern.
Scoped packages (@yourorg/mycli) require npm publish --access public for the first publish. Scoped packages are private by default and the --access public flag is easy to forget. Add it to your prepublishOnly check or use a .npmrc file with access=public scoped to your org.
For CLIs that need to run on machines without Node.js — enterprise deployment scripts, DevOps tools that need to work across different server configurations — standalone binaries via pkg or caxa package the Node.js runtime alongside your compiled code. The ~80MB binary includes everything needed to run. GitHub Releases is the right distribution channel for these: create a release, attach platform-specific binaries (mycli-macos-arm64, mycli-linux-x64, mycli-win-x64.exe), and let users download the correct binary for their system. A shell install script that detects the platform and downloads the right binary completes the experience.
For CLIs where automatic updates matter (you want users to always run the latest version), update-notifier is the standard package. It checks the npm registry for a newer version than what's installed and prints a message. The check runs in the background via a detached child process so it never adds latency to the CLI's actual execution. Combine this with a clear --version flag (which Commander and yargs both wire automatically from your package.json version) so users can always report what version they're running when filing bugs.
Interactive Prompts and Rich Terminal Output
Most production CLIs need more than argument parsing. Interactive prompts — asking users for confirmation, letting them select from a list, collecting sensitive input like passwords — are table stakes for deployment scripts and setup wizards. Rich output formatting — spinners during long operations, colored status messages, progress bars for file operations — significantly improves usability.
The @inquirer/prompts package (the modular successor to inquirer) is the standard for interactive prompts. It provides input, password, confirm, select, multi-select, and search prompts with accessible keyboard navigation:
import { input, password, select, confirm } from '@inquirer/prompts';
const projectName = await input({ message: 'Project name:' });
const dbPassword = await password({ message: 'Database password:' });
const environment = await select({
message: 'Deploy to which environment?',
choices: [
{ name: 'Staging', value: 'staging' },
{ name: 'Production', value: 'production', disabled: !hasProductionAccess },
],
});
const confirmed = await confirm({
message: `Deploy ${projectName} to ${environment}?`,
default: false,
});
if (!confirmed) {
console.log('Deployment cancelled');
process.exit(0);
}
For terminal output formatting, chalk handles colors and ora handles spinners:
import chalk from 'chalk';
import ora from 'ora';
// Semantic color usage — not decoration
console.log(chalk.green('✓'), 'Build completed');
console.log(chalk.yellow('⚠'), 'Using cached dependencies');
console.log(chalk.red('✗'), 'Deployment failed');
// Spinner for operations with unknown duration
const spinner = ora('Deploying to production...').start();
try {
await deploy();
spinner.succeed('Deployment complete');
} catch (err) {
spinner.fail(`Deployment failed: ${err.message}`);
process.exit(1);
}
The combination of a spinner (indicating something is happening) with a clear success/failure terminal state is a minimum viable UX for any CLI operation that takes more than a second. Users who run your CLI in CI systems often pipe output to log files — verify that your interactive prompts detect non-TTY environments (process.stdout.isTTY) and fall back to non-interactive behavior or throw a helpful error asking for the flag equivalent.