node-args

2.1.8 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates importing `node-args` and accessing the automatically parsed command-line arguments. Note how the module directly exports the parsed object rather than a parsing function.

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
*/

view raw JSON →