← All notes

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

Rules

See also

Claims

Sources

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

Permanent ID: iil:note:wckvrk0j

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.},
}

Markdown

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.

All artifact files (JSON index)

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.