Utilities

util.parseArgs()

Parses argument strings using explicit option definitions. Strict parsing rejects unknown options; separately validate domain values and required arguments. Supplying args makes tests independent of process.argv. Tested on Node.js 22.16.0. Example output (one line per entry): ["3", "ERR_PARSE_ARGS_UNKNOWN_OPTION"].

Formal Syntax & Structure

nodejs syntaxnodejs
util.parseArgs([config])

Parameters & Attributes

NameTypeDescription
configobjectArguments and option definitions.

Return Value

object

Values, positionals and optional tokens.

Working Example

Example Snippetnodejs
import { parseArgs } from 'node:util';const config = { options: { limit: { type: 'string' } }, strict: true };const r = parseArgs({ ...config, args: ['--limit', '3'] });console.log(r.values.limit);try {    parseArgs({ ...config, args: ['--typo'] });}catch (e) {    console.log(e.code);}
Node.js Reference