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.
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.
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
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:
--no-color → color: false)// yargs with type coercion
const argv = yargs
.option('port', { type: 'number' })
.parse(['--port', '8080']);
// argv.port is number 8080, not string '8080'
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
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;
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
minimist if:yargs if: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.
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.
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 be a node.js library fer hearties tryin' ter parse optstrings
Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.
It gives you:
my-program.js serve --port=5000).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]
Stable version:
npm i yargs
Bleeding edge version with the most recent features:
npm i yargs@next
#!/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:
hideBinis a shorthand forprocess.argv.slice(2). It has the benefit that it takes into account variations in some environments, e.g., Electron.
#!/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.
yargs has type definitions at @types/yargs.
npm i @types/yargs --save-dev
See usage examples in docs.
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
-denoflag on the end, like@17.7.2-deno
See examples of using yargs in the browser in docs.
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.