API Reference
Commander
Complete solution for Node.js command-line interfaces with subcommands, options, and help generation.
Official Documentation ↗CompatibilityThis documentation covers version
v12.1. Check the official docs for the latest API surface.Installation
npm
npm install commanderpnpm
pnpm add commanderyarn
yarn add commanderMethods
No methods match this filter.
Commander is a complete solution for building command-line interfaces in Node.js. It handles argument parsing, options with defaults and choices, subcommands, variadic args, and automatic help generation.
Key Features
- Subcommands: Nested commands with their own options and actions
- Options: Short/long flags, defaults, choices, and coercion
- Help: Auto-generated usage and help text
- Variadic args: Capture remaining arguments as arrays
- TypeScript: Bundled type definitions
Errors
The official API reference declares the following error types:
CommanderError(class) — official text: “export class CommanderError extends Error { code: string; exitCode: number; message: string; nestedError?: string; /** * Constructs the CommanderError class * @param exitCode - suggested exit code which could be used with process.exit * @param code - an id str” (source: https://github.com/tj/commander.js/blob/master/typings/index.d.ts, fetched 2026-11-08T16:01:12.9137723+08:00)InvalidArgumentError(class (extends CommanderError)) — official text: “export class InvalidArgumentError extends CommanderError { /** * Constructs the InvalidArgumentError class * @param message - explanation of why argument is invalid */ constructor(message: string); }” (source: https://github.com/tj/commander.js/blob/master/typings/index.d.ts, fetched 2026-11-08T16:01:12.9137723+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:01:12.9137723+08:00 via PowerShell Invoke-WebRequest 下载官方仓库 typings/index.d.ts(raw.githubusercontent.com/tj/commander.js/master),Grep/Read 提取原文.
.command→ signature from https://github.com/tj/commander.js/blob/master/typings/index.d.ts; official text: “command( nameAndArgs: string, opts?: CommandOptions, ): ReturnType<this[‘createCommand’]>;”.option→ signature from https://github.com/tj/commander.js/blob/master/typings/index.d.ts; official text: “option( flags: string, description?: string, defaultValue?: string | boolean | string[], ): this;”.action→ signature from https://github.com/tj/commander.js/blob/master/typings/index.d.ts; official text: “action(fn: (this: this, …args: any[]) => void | Promise): this;” .parse→ signature from https://github.com/tj/commander.js/blob/master/typings/index.d.ts; official text: “parse(argv?: readonly string[], parseOptions?: ParseOptions): this;”.addHelpCommand / .help→ signature from https://github.com/tj/commander.js/blob/master/typings/index.d.ts; official text: “command( nameAndArgs: string, opts?: CommandOptions, ): ReturnType<this[‘createCommand’]>;”
FAQ
- How do I get a typed options object in Commander?
- Call program.opts() after parse() to receive an object keyed by camelCased option names. For TypeScript, pass a generic like program.opts<{ port: string }>() or define an interface.
- How do subcommands work?
- Use program.command('name') to create a subcommand with its own options and action. For external subcommands, set executable: true and a command file path, letting Commander spawn them on demand.
- How do I show custom help?
- Override .addHelpText() or .configureHelp() to customize layout, sorting, and descriptions. You can also set .usage() and .description() and call .outputHelp() manually.
Report a problem with this API docOpens a pre-filled issue on GitHub.