yargs
Yargs is a feature-rich library for building interactive command-line tools by parsing arguments, generating help text, and providing a comprehensive set of uti…
Installation
npm install yargs
yarn add yargs
pnpm add yargs
Import
import yargs from 'yargs';
Quick Example
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv))
.command('serve', 'start server', (y) =>
y.option('port', { type: 'number', default: 3000 })
)
.demandCommand()
.parse();About yargs
Yargs is a feature-rich library for building interactive command-line tools by parsing arguments, generating help text, and providing a comprehensive set of utilities for CLI application development. Yargs excels at building complex CLI applications with multiple commands, nested subcommands, positional arguments, typed options with coercion, argument validation, middleware, and auto-generated help and completion scripts. The library supports commands with builder functions that define command-specific options, handlers that execute the command logic, and middleware that runs before handlers for tasks like loading configuration files or checking authentication. Yargs provides .demandOption() for required flags, .choices() for enum-like options, .conflicts() and .implies() for option relationships, .coerce() for type conversion, and .check() for custom validation. The .completion() method generates shell completion scripts for bash, zsh, and fish. Yargs handles configuration from environment variables, config files (JSON/YAML), and package.json fields. The library supports localized help text through a string table system. Yargs is particularly well-suited for complex CLI applications with multiple commands and sophisticated option relationships.
Quick Facts
| Package | yargs |
| Category | Utility |
| Weekly Downloads | 15M+ |
| License | MIT |
| Install | npm install yargs |
Related Packages
Commander is the most popular library for building command-line interfaces in Node.js, providing a f…
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.