node-getopt: Command Line Argument Parser
node-getopt is a minimalist command-line argument parser for Node.js, designed to mimic the traditional `getopt` utility. Currently at version 0.3.2, the package was last updated in 2013, indicating it is an abandoned project and no longer actively maintained. Its primary function is to define and parse both short and long options, supporting mandatory or optional arguments, and handling multiple occurrences of an option. It includes utilities for binding a 'help' option and generating usage messages. Its key differentiator at the time was its simplicity and close adherence to traditional `getopt` semantics for Node.js environments. Due to its age and reliance on very old Node.js versions (>=0.6.0), modern Node.js applications will likely find it incompatible or prefer more contemporary argument parsing libraries that support ESM and TypeScript.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require('node-getopt')` in an ES module file where `require` is not globally available.fixEnsure your file is treated as a CommonJS module (e.g., by using `.cjs` extension or `"type": "commonjs"` in `package.json`). Or, preferably, migrate to a modern CLI parser that supports ESM. -
Error: Cannot find module 'node-getopt'
cause The `node-getopt` package is not installed or not resolvable from the current execution environment.fixInstall the package using `npm install node-getopt` or `yarn add node-getopt`.
Warnings
- breaking This package is CommonJS-only and does not provide ES module exports. Attempting to `import` it in an ES module environment will result in a runtime error.
- gotcha The package has not been updated since 2013 (version 0.3.2) and targets Node.js versions >=0.6.0. It is unlikely to be compatible with modern Node.js features or best practices without significant issues or security concerns.
- gotcha There are no TypeScript type definitions available for `node-getopt` (neither bundled nor on `@types/node-getopt`). This means TypeScript projects will lack type safety and autocompletion when using this library.
Install
-
npm install node-getopt -
yarn add node-getopt -
pnpm add node-getopt
Imports
- Getopt
import Getopt from 'node-getopt';
const Getopt = require('node-getopt'); - create
import { create } from 'node-getopt';const getopt = require('node-getopt').create([ /* ... */ ]); - parseSystem
Getopt.parseSystem();
opt.parseSystem();
Quickstart
const Getopt = require('node-getopt');
// Create a Getopt instance with option definitions
const opt = Getopt.create([
['s' , '' , 'short option.'],
['' , 'long' , 'long option.'],
['S' , 'short-with-arg=ARG' , 'option with argument', 'S'],
['L' , 'long-with-arg=ARG' , 'long option with argument'],
['' , 'color[=COLOR]' , 'COLOR is optional'],
['m' , 'multi-with-arg=ARG+' , 'multiple option with argument'],
['' , 'no-comment'],
['h' , 'help' , 'display this help'],
['v' , 'version' , 'show version']
])
.bindHelp(); // Bind the 'help' option to display usage information
// Parse command line arguments from process.argv
const parsed = opt.parseSystem();
console.log('Parsed Options:', parsed.options);
console.log('Remaining Arguments:', parsed.argv);
/*
To run:
node your-script-name.js foo -s --long-with-arg bar -m a -m b -- --others
Example Output:
Parsed Options: { 'short-with-arg': 'S', s: true, 'long-with-arg': 'bar', 'multi-with-arg': [ 'a', 'b' ], S: 'S', L: 'bar', m: [ 'a', 'b' ] }
Remaining Arguments: [ 'foo', '--others' ]
*/