Registry / devops / nopt
library9.0.0jsnpmunverified

This library provides a robust and flexible option parsing solution for Node.js command-line interfaces. It is famously used by npm itself to handle its complex array of command-line arguments and configuration. `nopt` supports defining various option types (String, Number, Boolean, path, url, Date, Stream), shorthands for brevity, and mechanisms for handling unknown options. It maintains a consistent release cadence, with major versions frequently aligning with Node.js LTS cycles. The current stable version, `9.0.0`, requires Node `^20.17.0 || >=22.9.0`. Its key differentiators include strong type definition capabilities, automatic path resolution, and a battle-tested core proven by its integration into the npm CLI.

npm install nopt
INSTALL
IMPORT
SIG · NOPT
N
nopt
devopsjavascriptv9.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nopt
import nopt from 'nopt'
import { nopt } from 'nopt'
While v9.0.0 supports modern Node.js, `nopt` is a CommonJS module. When importing into an ESM context, use a default import.
nopt
const nopt = require('nopt')
This is the canonical CommonJS import method shown in the official documentation and examples.
nopt.typeDefs
import nopt from 'nopt'; const types = nopt.typeDefs;
import { typeDefs } from 'nopt'
`typeDefs` is a property of the main `nopt` function, not a separate export.

Demonstrates basic option parsing using `nopt`, defining known options with types and shorthands, and processing `process.argv` to extract command-line arguments into a structured object.

const nopt = require('nopt'); const Stream = require('stream').Stream; const path = require('path'); const knownOpts = { "foo": [String, null], "bar": [Stream, Number], "baz": path, "bloo": ["big", "medium", "small"], "flag": Boolean, "pick": Boolean, "many1": [String, Array], "many2": [path, Array] }; const shortHands = { "foofoo": ["--foo", "Mr. Foo"], "b7": ["--bar", "7"], "m": ["--bloo", "medium"], "p": ["--pick"], "f": ["--flag"] }; // nopt takes known options, shorthands, the argument list (defaults to process.argv), // and a slice index (defaults to 2 for process.argv). const parsed = nopt(knownOpts, shortHands, process.argv, 2); console.log('Parsed Options:', parsed); console.log('Remaining Arguments:', parsed.argv.remain); // Example usage: // node your-script.js --foo "blerp" --no-flag // node your-script.js --blatzk=1000 -fp
Debug
Known issues
breakingVersion `9.0.0` introduces a breaking change by tightening Node.js engine compatibility. It now strictly requires Node.js `^20.17.0 || >=22.9.0`.
fix
Ensure your project's Node.js environment meets the `^20.17.0 || >=22.9.0` requirement. Upgrade Node.js if necessary.
affects: >=9.0.0
breakingVersion `8.0.0` introduced a breaking change by raising the minimum Node.js engine compatibility to `^18.17.0 || >=20.5.0`.
fix
Verify your Node.js version is `^18.17.0 || >=20.5.0`. Older versions will no longer be supported.
affects: >=8.0.0 <9.0.0
breakingVersion `7.0.0` dropped support for older Node.js versions. The compatible range became `^14.17.0 || ^16.13.0 || >=18.0.0`.
fix
Update your Node.js runtime to one within the `^14.17.0 || ^16.13.0 || >=18.0.0` range.
affects: >=7.0.0 <8.0.0
breakingVersion `6.0.0` significantly changed Node.js support, dropping compatibility with Node.js versions 6 through 10, and non-LTS versions of 12 and 14.
fix
If upgrading from `nopt@5.x` or older, ensure your Node.js version is at least 12.x LTS or 14.x LTS.
affects: >=6.0.0 <7.0.0
deprecatedThe `String.prototype.substr()` method, which `nopt` previously used internally, has been deprecated in JavaScript. While `nopt` fixed this in v6.0.0, older versions might trigger deprecation warnings in modern Node.js environments.
fix
Upgrade to `nopt@6.0.0` or newer to avoid deprecated API usage warnings.
affects: <6.0.0
Errors
Common errors & fixes
Error: Cannot find module 'nopt'
The 'nopt' package is not installed or the Node.js module resolution path is incorrect.
fix
Ensure 'nopt' is installed as a dependency: `npm install nopt` or `yarn add nopt`. Verify your `NODE_PATH` if using custom module paths.
TypeError: nopt is not a function
Attempting to import `nopt` incorrectly as a named export in an ESM context, or a corrupted `node_modules` installation.
fix
For ESM, use `import nopt from 'nopt';`. For CommonJS, use `const nopt = require('nopt');`. If the issue persists, try deleting `node_modules` and `package-lock.json` and reinstalling dependencies.
The package "nopt" vX.Y.Z does not satisfy its "engines" property.
The installed Node.js version does not meet the `engines` requirements specified in `nopt`'s `package.json`.
fix
Upgrade your Node.js environment to a version compatible with `nopt`'s requirements (e.g., `^20.17.0 || >=22.9.0` for v9.0.0). Consider using `nvm` or `volta` to manage Node.js versions.
Upgrade
Version history
9.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
nopt — npm install nopt · libregistry