yargs-parser is the robust, highly configurable option parser underlying the popular yargs command-line interface library. Currently at stable version 22.0.0, it follows a release cadence tied to yargs itself, with major versions introducing breaking changes like the recent shift to ESM-first. Its key differentiators include comprehensive argument parsing capabilities, extensive configuration options for aliases, types (boolean, number, string, array), defaults, environment variable handling, and support for configuration files. It operates efficiently across various JavaScript environments, including Node.js (requiring Node.js ^20.19.0 || ^22.12.0 || >=23 as of v22), web browsers via specific builds, and Deno, making it a versatile choice for parsing CLI arguments or arbitrary strings.
npm install yargs-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing command-line arguments using various configuration options like types, aliases, defaults, and custom coercions.
Migrate your project to use ES Modules `import` syntax. If targeting older Node.js or strictly CommonJS, consider pinning to a version prior to 22.0.0 or configuring a build step (e.g., Babel, TypeScript) for CJS output from ESM source.
Upgrade your Node.js runtime to a supported version. Refer to the `engines.node` field in the package.json for exact requirements.
If experiencing slow parsing, especially with many arguments, consider refactoring your CLI to explicitly define options rather than relying heavily on `unknown-options-as-args`. Profile your application to identify bottlenecks.
Ensure that any array passed to `parser()` is first converted to an array of strings, e.g., `argsArray.map(String)` or `argsArray.join(' ')` if you want to parse a single string.Change your import statement to `import parser from 'yargs-parser'`.
Ensure your `tsconfig.json`'s `module` and `moduleResolution` settings align with your runtime environment (e.g., `"module": "NodeNext"`, `"moduleResolution": "NodeNext"` for modern Node.js ESM projects). Also, explicitly use `import parser from 'yargs-parser'` for ESM.
Explicitly define the expected types for your arguments using the `string`, `number`, `boolean`, `array`, or `coerce` options in the `parser`'s second argument (`opts`). For example, `{ number: ['port'], string: ['host'] }`.No dependency data recorded yet.