Node.js util.parseArgs: parse command-line flags
Answer: Import parseArgs from node:util, describe each option with a type of boolean or string, and read the returned values and positionals. It is a higher level API for argument parsing than reading process.argv yourself.
Examples
import { parseArgs } from 'node:util';
const { values, positionals } = parseArgs({
options: {
verbose: { type: 'boolean', short: 'v' },
out: { type: 'string', short: 'o', default: 'dist' },
tag: { type: 'string', multiple: true },
color: { type: 'boolean' },
},
allowPositionals: true,
allowNegative: true, // --no-color
});
// node cli.js -v --tag a --tag b --no-color src
// values: { verbose: true, out: 'dist', tag: ['a', 'b'], color: false }
// positionals: ['src']
try {
parseArgs({ args: ['--nope'], options: {} });
} catch (err) {
console.error(err.code); // unknown option
}
Config
| Property | Use | |---|---| | args | argument strings; default is process.argv with execPath and filename removed | | options | keys are the long names of options | | strict | throw on unknown arguments or a type mismatch; default true | | allowPositionals | accept positional arguments; default false if strict is true | | allowNegative | set boolean options to false by prefixing the option name with --no- | | tokens | return the parsed tokens for custom behavior |
Option fields
type: must be eitherbooleanorstring.short: a single character alias.multiple: if true, values are collected in an array; otherwise the last value wins.default: applied only when the option does not appear, and it must match the type.
Rules
- The result has
values, a mapping of parsed option names, andpositionals. - Short option groups like
-xyexpand to a token for each option. - With
tokens: truethe returned tokens can be reprocessed, for example to add checks.
See also
Claims
- id: C1 claim: parseArgs is a higher level API than process.argv. source: S1 evidence: "Provides a higher level API for command-line argument parsing than interacting with process.argv directly."
- id: C2 claim: type must be boolean or string. source: S1 evidence: "Type of argument, which must be either boolean or string ."
- id: C3 claim: Non-multiple options are last-wins. source: S2 evidence: "values for the option are last-wins."
Sources
- [S1] Util - util.parseArgs. Node.js API docs. https://nodejs.org/api/util.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/util.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/util.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Util - util.parseArgs; nodejs/node (GitHub), doc/api/util.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Util - util.parseArgs; nodejs/node (GitHub), doc/api/util.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.
Cite this
iinventedlight. 2026. "Node.js util.parseArgs: parse command-line flags." iinventedlight Library, iil:note:wckvrk0j, version of 2026-11-18. https://iinventedlight.com/library/notes/node-util-parseargs/. License: CC-BY-4.0.
@misc{iil_note_wckvrk0j,
author = {{iinventedlight}},
title = {{Node.js util.parseArgs: parse command-line flags}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-util-parseargs/},
note = {Permanent ID iil:note:wckvrk0j. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- node-util-parseargs.table.json (table; CC-BY-4.0)
- node-util-parseargs.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-util-parseargs.md.