yargs vs minimist
Command-Line Argument Parsing in Node.js
yargsminimistSimilar Packages:

Command-Line Argument Parsing in Node.js

minimist and yargs are both Node.js libraries for parsing command-line arguments, but they target different levels of complexity. minimist is a minimal, zero-dependency parser that converts process.argv into a simple object with basic flag support. yargs is a feature-rich CLI framework that provides automatic help generation, input validation, type coercion, subcommands, and a polished developer experience for building robust command-line tools.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
yargs148,849,43511,458231 kB30910 months agoMIT
minimist066254.5 kB143 years agoMIT

Command-Line Argument Parsing in Node.js: minimist vs yargs

Both minimist and yargs help you parse command-line arguments in Node.js applications, but they serve very different needs. Let’s compare them based on real-world usage patterns.

📦 Core Philosophy: Minimalism vs Full-Featured

minimist is a tiny, zero-dependency parser that turns process.argv into a plain object. It does one thing and does it well — no frills, no validation, no help text.

// Using minimist
const minimist = require('minimist');
const args = minimist(process.argv.slice(2));
// Input: node app.js --port=3000 --verbose
// Output: { _: [], port: 3000, verbose: true }

yargs is a full-featured CLI builder. It handles parsing, validation, auto-generated help menus, subcommands, and more out of the box.

// Using yargs
const yargs = require('yargs');
const argv = yargs
  .option('port', {
    describe: 'Port to run server on',
    type: 'number',
    default: 3000
  })
  .help()
  .argv;
// Automatically shows help if --help is passed

🔤 Argument Syntax Support

Both support short (-p) and long (--port) flags, but yargs goes further.

minimist supports basic syntax:

// minimist examples
minimist(['-p', '8080']);           // { _: [], p: '8080' }
minimist(['--port', '8080']);        // { _: [], port: '8080' }
minimist(['--no-color']);            // { _: [], color: false }
minimist(['--files', 'a.txt', 'b.txt']); // { _: [], files: ['a.txt', 'b.txt'] }

yargs supports all of the above plus:

  • Boolean negation (--no-colorcolor: false)
  • Type coercion (strings to numbers/booleans)
  • Dot notation for nested objects
  • Strict mode (fail on unknown options)
// yargs with type coercion
const argv = yargs
  .option('port', { type: 'number' })
  .parse(['--port', '8080']);
// argv.port is number 8080, not string '8080'

🧾 Help Text and UX

minimist provides no help generation. You must write and print usage instructions yourself.

// With minimist, you manage help manually
if (args.help) {
  console.log('Usage: app.js --port <number>');
  process.exit(0);
}

yargs auto-generates formatted help based on your configuration:

// yargs auto-help
yargs
  .option('port', {
    alias: 'p',
    describe: 'Server port',
    type: 'number'
  })
  .demandOption(['port'])
  .help()
  .argv;
// Running `node app.js --help` prints a clean usage guide

🧩 Subcommands and Complex CLIs

minimist has no built-in support for subcommands (e.g., git commit, git push). You’d need to manually split args._ and route logic.

// Manual subcommand handling with minimist
const args = minimist(process.argv.slice(2));
const cmd = args._[0];
if (cmd === 'build') { /* ... */ }
else if (cmd === 'serve') { /* ... */ }

yargs has first-class subcommand support:

// yargs subcommands
yargs
  .command('build [input]', 'Build project', (yargs) => {
    yargs.positional('input', { type: 'string' });
  }, (argv) => {
    console.log('Building', argv.input);
  })
  .command('serve', 'Start dev server', () => {}, () => {
    console.log('Serving...');
  })
  .argv;

⚠️ Validation and Error Handling

minimist performs no validation. All values are strings or booleans based on presence. You must validate types, required fields, and ranges yourself.

// With minimist, manual validation
const args = minimist(process.argv.slice(2));
if (typeof args.port !== 'number') {
  throw new Error('--port must be a number');
}

yargs validates types, enforces required options, and shows user-friendly errors:

// yargs validation
yargs
  .option('port', {
    type: 'number',
    demandOption: true,
    coerce: (arg) => {
      if (arg < 1 || arg > 65535) throw new Error('Port out of range');
      return arg;
    }
  })
  .argv;
// Fails with clear message if --port is missing or invalid

🛠️ When to Use Which?

Use minimist if:

  • You’re building a tiny script or internal tool
  • You only need basic flag parsing
  • You want zero dependencies and minimal overhead
  • You’re comfortable handling help text, validation, and UX manually

Use yargs if:

  • You’re building a public CLI tool or developer-facing utility
  • You need auto-generated help, validation, or subcommands
  • You want a polished user experience with minimal effort
  • Your CLI has complex options or multiple modes of operation

📌 Final Thought

minimist is like a pocket knife — small, sharp, and always handy for quick tasks. yargs is like a full toolbox — heavier, but ready for any job. Choose based on how much CLI infrastructure your project actually needs.

How to Choose: yargs vs minimist

  • yargs:

    Choose yargs if you're developing a public-facing CLI tool, a developer utility, or any application where user experience matters. It saves significant time by providing auto-generated help, strict validation, type coercion, subcommand support, and error messages that guide users — all with minimal configuration.

  • minimist:

    Choose minimist if you're building a small internal script or utility where you only need basic argument parsing without help text, validation, or subcommands. It’s ideal when you want zero dependencies, minimal bundle impact, and are comfortable handling edge cases and user guidance manually.

README for yargs

Yargs

Yargs be a node.js library fer hearties tryin' ter parse optstrings


ci NPM version js-standard-style Coverage Conventional Commits

Description

Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.

It gives you:

  • commands and (grouped) options (my-program.js serve --port=5000).
  • a dynamically generated help menu based on your arguments:
mocha [spec..]

Run tests with Mocha

Commands
  mocha inspect [spec..]  Run tests with Mocha                         [default]
  mocha init <path>       create a client-side Mocha setup at <path>

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]
  --async-only, -A           Require all tests to use a callback (async) or
                             return a Promise                          [boolean]
  • generate completion scripts for Bash and Zsh for your command
  • and tons more.

Installation

Stable version:

npm i yargs

Bleeding edge version with the most recent features:

npm i yargs@next

Usage

Simple Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).parse()

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

Note: hideBin is a shorthand for process.argv.slice(2). It has the benefit that it takes into account variations in some environments, e.g., Electron.

Complex Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

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()

Run the example above with --help to see the help for the application.

Supported Platforms

TypeScript

yargs has type definitions at @types/yargs.

npm i @types/yargs --save-dev

See usage examples in docs.

Deno

As of v16, yargs supports Deno:

import yargs from 'https://deno.land/x/yargs@v17.7.2-deno/deno.ts'
import { Arguments } from 'https://deno.land/x/yargs@v17.7.2-deno/deno-types.ts'

yargs(Deno.args)
  .command('download <files...>', 'download a list of files', (yargs: any) => {
    return yargs.positional('files', {
      describe: 'a list of files to do something with'
    })
  }, (argv: Arguments) => {
    console.info(argv)
  })
  .strictCommands()
  .demandCommand(1)
  .parse()

Note: If you use version tags in url then you also have to add -deno flag on the end, like @17.7.2-deno

Usage in Browser

See examples of using yargs in the browser in docs.

Documentation

Table of Contents

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.