minimist vs yargs
Command-Line Argument Parsing in Node.js
minimistyargsSimilar 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
minimist066254.5 kB144 years agoMIT
yargs011,514235 kB216a month 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-color → color: 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: minimist vs yargs

  • 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.

  • 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.

README for minimist

minimist Version Badge

github actions coverage License Downloads

npm badge

parse argument options

This module is the guts of optimist's argument parser without all the fanciful decoration.

example

var argv = require('minimist')(process.argv.slice(2));
console.log(argv);
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{
	_: ['foo', 'bar', 'baz'],
	x: 3,
	y: 4,
	n: 5,
	a: true,
	b: true,
	c: true,
	beep: 'boop'
}

security

Previous versions had a prototype pollution bug that could cause privilege escalation in some circumstances when handling untrusted user input.

Please use version 1.2.6 or later:

methods

var parseArgs = require('minimist')

var argv = parseArgs(args, opts={})

Return an argument object argv populated with the array arguments from args.

argv._ contains all the arguments that didn't have an option associated with them.

Numeric-looking arguments will be returned as numbers unless opts.string or opts.boolean is set for that argument name.

Any arguments after '--' will not be parsed and will end up in argv._.

options can be:

  • opts.string - a string or array of strings argument names to always treat as strings

  • opts.boolean - a boolean, string or array of strings to always treat as booleans. if true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar)

  • opts.alias - an object mapping string names to strings or arrays of string argument names to use as aliases

  • opts.default - an object mapping string argument names to default values

  • opts.stopEarly - when true, populate argv._ with everything after the first non-option

  • opts['--'] - when true, populate argv._ with everything before the -- and argv['--'] with everything after the --. Here's an example:

    > require('./')('one two three -- four five --six'.split(' '), { '--': true })
    {
      _: ['one', 'two', 'three'],
      '--': ['four', 'five', '--six']
    }
    

    Note that with opts['--'] set, parsing for arguments still stops after the --.

  • opts.unknown - a function which is invoked with a command line parameter not defined in the opts configuration object. If the function returns false, the unknown option is not added to argv.

install

With npm do:

npm install minimist

license

MIT