commander
Commander is the most popular library for building command-line interfaces in Node.js, providing a fluent API for defining commands, options, and arguments. Com…
Installation
npm install commander
yarn add commander
pnpm add commander
Import
import { Command } from 'commander';Quick Example
import { Command } from 'commander';
const program = new Command();
program
.name('my-cli')
.option('-p, --port <number>', 'port', '3000')
.option('-d, --debug', 'debug mode')
.parse();
const opts = program.opts();
console.log(`Port: ${opts.port}`);About commander
Commander is the most popular library for building command-line interfaces in Node.js, providing a fluent API for defining commands, options, and arguments. Commander automatically generates help output from your program definition, parses process.argv, validates inputs, and displays error messages for invalid usage. The library supports subcommands with their own options and help, variadic arguments, required and optional options with default values, boolean flags, option value parsing/coercion, environment variable defaults, and custom help formatting. Commander handles common CLI patterns like version flags (--version), help flags (--help), and unknown option detection. The API is intuitive: program.option('-p, --port <number>', 'port number', '3000') defines a port option with description and default. Commander supports action handlers for subcommands, hooks for pre/post processing, and configurable error handling. The library is used by major CLI tools including Vue CLI, webpack-cli, create-react-app, and npm itself. Commander provides TypeScript declarations and works with both CommonJS and ESM. For simple CLIs, Commander's declarative approach is more concise than yargs's chaining style.
Quick Facts
| Package | commander |
| Category | Utility |
| Weekly Downloads | 20M+ |
| License | MIT |
| Install | npm install commander |
Related Packages
Yargs is a feature-rich library for building interactive command-line tools by parsing arguments, ge…
Inquirer.js is a collection of common interactive command-line user interfaces for Node.js, providin…
Chalk is the most popular library for styling terminal string output with colors and formatting in N…
Ora is an elegant terminal spinner library for Node.js that provides visual feedback during long-run…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.