yargs: Argument Parsing and Auto-Help for Node.js CLI Tools
yargs the modern, pirate-themed successor to optimist.
At a glance
- What is it?
- yargs is a Node.js library for parsing command-line arguments and building interactive CLI tools. It generates a help menu automatically from declared options, supports nested command structures, and works with TypeScript, Deno, and browser environments.
- Who is it for?
- yargs is the right choice for Node.js CLI developers who want auto-generated help text, structured command trees, and TypeScript types without writing the argument parsing logic by hand. It is not the right choice for scripts where binary size matters or for environments where its dependency chain is a concern.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 1 day ago.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on September 29, 2026, and from our analysis. They are not legal advice.
Editorial analysis
What yargs Solves in Node.js CLI Development
Writing a command-line tool in Node.js involves parsing process.argv, handling flags, validating inputs, and writing a usage message. yargs handles all of this from a single fluent API. The README describes yargs as helping developers build interactive command-line tools by parsing arguments and generating an elegant user interface. The library produces a help menu automatically from declared options and commands, which means the --help output stays in sync with the declared API without manual updates. It supports commands with their own sub-options, positional arguments with default values, grouped option sections in the help output, and Bash and Zsh completion script generation. The package is published on npm as yargs, and the current version shown in package.json is 18.2.0.
Installing yargs and a First Example
Install the stable version from npm.
npm i yargsThe README also shows how to install the bleeding-edge pre-release with npm i yargs@next. A minimal script parses flags directly from process.argv. The hideBin helper from yargs/helpers strips the first two elements (node and the script path) from process.argv.
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).parse()With this setup, running the script with --ships=4 --distance=22 populates argv.ships and argv.distance. The README notes that hideBin also handles variations in some environments, such as Electron, which modifies the process.argv layout. No additional configuration is required to parse basic key=value and boolean flags.
Commands, Positional Arguments, and Nested Structures
yargs supports a command model where each subcommand has its own handler function and option declarations. The .command method takes a command name pattern, a description, a builder function for the command's options, and a handler function.
yargs(hideBin(process.argv))
.command('serve [port]', 'start the server', (yargs) => {
return yargs
.positional('port', {
describe: 'port to bind on',
default: 5000
})
}, (argv) => {
if (argv.verbose) console.info(`start server on :${argv.port}`)
serve(argv.port)
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging'
})
.parse()Positional arguments use bracket notation for optional (serve [port]) and angle bracket notation for required (serve <port>). Passing --help to the above example generates formatted output showing the serve command, the port positional argument, and the verbose option, all derived from the declared metadata without writing a usage string manually.
Auto-Generated Help and Completion Scripts
The help menu that yargs generates from declared options and commands is one of its primary features. The README shows an example from mocha's yargs-based CLI that includes a Commands section listing subcommands, a Rules and Behavior section grouping boolean flags, and type annotations on each option. This output is produced entirely from the declarations, not from manually written text. yargs also generates Bash and Zsh completion scripts for a CLI tool. The completion script understands the command structure declared in yargs and provides tab-completion for commands, options, and option values. The README notes that extensive additional functionality is documented at the API reference in docs/api.md, with parsing tricks in docs/tricks.md and advanced topics in docs/advanced.md.
TypeScript, Deno, and Browser Support
TypeScript definitions for yargs are available from DefinitelyTyped. The README instructs installing them as a development dependency.
npm i @types/yargs --save-devThe package.json exports map exposes a ./helpers entry point and a ./browser entry point in addition to the root. The browser entry point, at browser.mjs, allows yargs to run in browser environments for web-based CLI tools or testing. Deno support was added in v16. The README provides a Deno import example using the versioned URL at deno.land/x/yargs. The package type is module in package.json, meaning the main entry point is an ES module. The project tracks the Node.js release schedule as described in the README, which means it supports the Node.js versions that are in current or LTS status rather than guaranteeing support for older versions.
Limitations: Dependency Footprint and Learning Curve
yargs installs several dependencies: cliui for terminal layout, escalade for directory traversal, get-caller-file, string-width, y18n for internationalization, and yargs-parser as the underlying parse engine. For minimal scripts where dependency count matters, this footprint is larger than simpler alternatives. The fluent API with chained .command, .option, and .parse calls works well for small CLIs but can become harder to follow in large codebases where commands are spread across many files. The README's documentation points to multiple doc files at docs/api.md, docs/tricks.md, and docs/advanced.md, which means a new user needs to navigate several pages to find specific behavior. Async command handlers require specific patterns documented in the advanced topics file.
yargs versus commander
commander is another widely used Node.js CLI argument parsing library. Both yargs and commander handle commands, options, and help generation. The primary difference in approach is design philosophy: yargs is built around parsing and auto-generating documentation from declaration metadata, with a fluent chaining API. commander uses a more explicit, object-oriented model where you create a Program instance and attach commands and options to it. yargs provides the hideBin helper, middleware, and built-in positional argument handling that commander handles differently. For TypeScript projects, both have type definitions, but yargs ships its configuration through @types/yargs while commander bundles its own types. The choice between them is largely a matter of which API style matches the project's conventions.
Editorial conclusion
yargs is the right choice for Node.js CLI developers who want auto-generated help text, structured command trees, and TypeScript types without writing the argument parsing logic by hand. It is not the right choice for scripts where binary size matters or for environments where its dependency chain is a concern. Before migrating an existing tool, check the v17 to v18 changelog since v18.2.0 is on npm and the API surface has evolved; also verify that your Node.js version is in the supported range listed in the project's release schedule policy.
Frequently asked questions
How do I use yargs in Node.js?
Install with npm i yargs, then import yargs and the hideBin helper. Call yargs(hideBin(process.argv)) to create a parser, chain .option or .command calls to declare your CLI's interface, and call .parse() at the end to run the parser and populate the argv object.
How do I install yargs?
Run npm i yargs for the stable release or npm i yargs@next for the bleeding-edge version. TypeScript definitions are available separately with npm i @types/yargs --save-dev.
What is yargs?
yargs is a Node.js library that parses command-line arguments and generates interactive help menus. It supports commands, positional arguments, type validation, Bash and Zsh completion scripts, and works with TypeScript, Deno, and browser environments.
How does yargs compare to commander?
Both yargs and commander parse CLI arguments and generate help text. yargs uses a fluent chaining API and builds documentation from declared metadata automatically. commander uses a more explicit object-oriented model. Both support TypeScript, commands, and options; the choice depends on which API style fits your project's conventions.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/yargs-yargs)