node-args
node-args is a minimalistic command-line argument parser for Node.js, currently at version 2.1.8. It distinguishes itself by providing a no-configuration, no-options approach to parsing `process.argv`. Unlike more feature-rich alternatives like `commander` or `yargs`, `node-args` automatically processes flags (`-s`, `--long`) and values, returning a simple object without requiring schema definitions or explicit parsing function calls. Its core design principle is simplicity, offering only the bare essentials for argument processing. The library appears to be in a maintenance state, having a stable API that has not changed significantly, and development focuses on stability rather than new features, aligning with its 'minimalistic' promise. It ships with TypeScript types, enhancing developer experience for typed projects.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS module file (e.g., a `.js` file without `"type": "module"` in `package.json`, or an older Node.js environment).fixEnsure your project is configured for ESM by adding `"type": "module"` to your `package.json` or explicitly naming your file with a `.mjs` extension. Alternatively, use CommonJS `require()` syntax: `const parsedArgs = require('node-args');` -
TypeError: require(...) is not a function
cause The `node-args` module directly exports the parsed arguments object, not a function to be called. Users often try to invoke it like a function: `require('node-args')()`.fixAccess the parsed arguments directly without calling it: `const parsedArgs = require('node-args');` -
My command-line arguments are not being parsed correctly when I change `process.argv` dynamically.
cause `node-args` parses `process.argv` at the moment it is loaded (via `require` or `import`), not when explicitly invoked. Any changes to `process.argv` *after* `node-args` has been loaded will be ignored.fixEnsure `node-args` is imported/required only after `process.argv` has been fully set up (e.g., in the main script file after `process.argv` is modified). For more flexible parsing, use a library that provides an explicit parsing function like `commander.js` or `yargs`.
Warnings
- gotcha `node-args` parses `process.argv` immediately upon `require()` or `import`. This means that any modifications to `process.argv` after the module is loaded will not be reflected in the parsed arguments. For testing or dynamic argument scenarios, this behavior can be a significant footgun.
- gotcha The library offers no configuration or schema for argument definition, validation, or type enforcement. Argument values are heuristically parsed (e.g., 'false' becomes boolean `false`, '2' becomes number `2`). This can lead to unexpected type conversions or incorrect assumptions about input.
- gotcha `node-args` is designed for basic flat argument parsing and does not support advanced CLI patterns such as subcommands (e.g., `git commit`), command-specific options, or generating help messages. It provides a simple key-value object of parsed arguments.
- gotcha The `_` (underscore) property in the parsed object contains an array of non-flag arguments, including the Node.js executable path (`process.argv[0]`) and the script file path (`process.argv[1]`), followed by any additional non-flag arguments. Developers often expect `_` to contain only unparsed application-specific arguments.
Install
-
npm install node-args -
yarn add node-args -
pnpm add node-args
Imports
- parsedArgs
const args = require('node-args')();const parsedArgs = require('node-args'); - parsedArgs
import { parsedArgs } from 'node-args';import parsedArgs from 'node-args';
- ArgsObject
import type { ArgsObject } from 'node-args';
Quickstart
import parsedArgs from 'node-args'; // Or: const parsedArgs = require('node-args');
// To simulate command-line arguments for testing, you might modify process.argv
// However, node-args parses process.argv AT THE TIME IT IS REQUIRED/IMPORTED.
// To demonstrate, we'll run a script with arguments.
// In a file named `my-script.ts` (or `.js`):
// console.log(parsedArgs);
// To run from the terminal:
// npx ts-node my-script.ts -t -ab=2 -c false -p no some additional data 2 --argsis awesome --another=1
// Expected output would be similar to:
/*
{
_: ['node', '/path/to/my-script.ts', 'some', 'additional', 'data', 2],
t: true,
a: 2,
b: 2,
c: false,
p: 'no',
argsis: 'awesome',
another: 1
}
*/
// For a runnable example within the registry format:
// We cannot dynamically alter process.argv for a quickstart that runs in a sandbox,
// but this code illustrates the usage if run from the command line.
// In a standalone script:
const actualParsedArgs = require('node-args');
console.log(actualParsedArgs);
/* Example of how you would run this:
node -r ts-node/register your-script.ts -t -ab=2 -c false -p no some additional data 2 --argsis awesome --another=1
*/