Getopts CLI Argument Parser
getopts is a lightweight and performant CLI argument parser for Node.js, currently at stable version 2.3.0. It aims to be a drop-in replacement for `minimist` and similar libraries, distinguishing itself through its small footprint (180 LOC), zero dependencies, and significantly faster parsing speeds, reportedly up to 6 times faster than alternatives. The library follows established utility conventions for argument parsing, providing sane defaults for common CLI patterns. It transitioned to ES modules in version 2.3.0 and has shipped TypeScript types since 2.1.1, facilitating modern JavaScript and TypeScript development workflows. Releases are generally driven by new features, bug fixes, and maintenance, with a recent focus on ESM migration.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to import `getopts` using CommonJS `require()` syntax in an ES module environment after upgrading to v2.3.0.fixChange `const getopts = require('getopts')` to `import getopts from 'getopts'` and ensure your project is configured for ESM. -
TypeError: getopts is not a function
cause This error can occur if you are trying to use a CommonJS `require()` for an ESM package, or if you are incorrectly trying to destructure a default export in a CommonJS context after v2.3.0.fixFor ESM, use `import getopts from 'getopts'`. For CommonJS, either stick to v2.2.x or use dynamic import: `(await import('getopts')).default` to access the default export. -
My command-line option is being parsed as a boolean (true/false) instead of a string value.
cause By default, short options without an immediately adjacent value or the `opts.string` configuration are treated as booleans. Similarly, long options without an `=` sign are boolean.fixTo force an option to be parsed as a string, use `opts.string: ['optionName']`. To force it as a boolean (and push any subsequent argument into the operand array `_`), use `opts.boolean: ['optionName']`. -
My default options object is being modified after parsing.
cause Prior to v2.0.0, `getopts` might have mutated the `options.default` object. Since v2.0.0, it explicitly does not.fixEnsure you are on `getopts@2.0.0` or newer. If you need to modify the default options based on parsed arguments, create a deep copy of your default options object before passing it to `getopts`.
Warnings
- breaking Version 2.3.0 migrated getopts to be an ES module (ESM) only. Projects using CommonJS `require()` will encounter `ReferenceError: require is not defined` or similar import errors.
- breaking Version 2.0.0 introduced several behavioral changes, notably parsing a standalone hyphen (`-`) as an operand and explicitly ceasing mutation of the `options.default` object. Code relying on previous behavior for these aspects will break.
- gotcha Options specified in `opts.string` or `opts.boolean` will be included in the result object with default values (`""` or `false`) even if they are not present in the `argv` array.
- gotcha Short option parsing can be ambiguous: `-a alpha` parses `a` as a string, but `-a` (followed by other options or end of arguments) parses `a` as a boolean. This can lead to unexpected type parsing.
- gotcha If an option is provided multiple times (e.g., `-x A -x B`), its value in the result object will be an array containing all occurrences in order, not just the last one.
Install
-
npm install getopts -
yarn add getopts -
pnpm add getopts
Imports
- getopts
const getopts = require('getopts')import getopts from 'getopts'
- Options
import type { Options } from 'getopts'
Quickstart
import getopts from "getopts";
const args = process.argv.slice(2);
const options = getopts(args, {
alias: {
output: ["o", "f"],
type: "t",
},
boolean: ["verbose"],
string: ["name"]
});
console.log("Parsed options:", options);
// Example usage: node your-script.js --type=module -o main.js *.{js,json} --name="My App" -v
// Expected output: { _: ['*.{js,json}'], output: 'main.js', type: 'module', o: 'main.js', f: 'main.js', t: 'module', name: 'My App', verbose: true }