🛠️
Utility20M+/wkMIT

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
npm install commander
yarn
yarn add commander
pnpm
pnpm add commander

Import

ESM
import { Command } from 'commander';

Quick Example

usage
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

Packagecommander
CategoryUtility
Weekly Downloads20M+
LicenseMIT
Installnpm install commander

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.