{"id":13681,"library":"nopt","title":"Node.js Option Parser (nopt)","description":"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.","status":"active","version":"9.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/npm/nopt","tags":["javascript"],"install":[{"cmd":"npm install nopt","lang":"bash","label":"npm"},{"cmd":"yarn add nopt","lang":"bash","label":"yarn"},{"cmd":"pnpm add nopt","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While v9.0.0 supports modern Node.js, `nopt` is a CommonJS module. When importing into an ESM context, use a default import.","wrong":"import { nopt } from 'nopt'","symbol":"nopt","correct":"import nopt from 'nopt'"},{"note":"This is the canonical CommonJS import method shown in the official documentation and examples.","symbol":"nopt","correct":"const nopt = require('nopt')"},{"note":"`typeDefs` is a property of the main `nopt` function, not a separate export.","wrong":"import { typeDefs } from 'nopt'","symbol":"nopt.typeDefs","correct":"import nopt from 'nopt'; const types = nopt.typeDefs;"}],"quickstart":{"code":"const nopt = require('nopt');\nconst Stream = require('stream').Stream;\nconst path = require('path');\n\nconst knownOpts = {\n  \"foo\": [String, null],\n  \"bar\": [Stream, Number],\n  \"baz\": path,\n  \"bloo\": [\"big\", \"medium\", \"small\"],\n  \"flag\": Boolean,\n  \"pick\": Boolean,\n  \"many1\": [String, Array],\n  \"many2\": [path, Array]\n};\n\nconst shortHands = {\n  \"foofoo\": [\"--foo\", \"Mr. Foo\"],\n  \"b7\": [\"--bar\", \"7\"],\n  \"m\": [\"--bloo\", \"medium\"],\n  \"p\": [\"--pick\"],\n  \"f\": [\"--flag\"]\n};\n\n// nopt takes known options, shorthands, the argument list (defaults to process.argv),\n// and a slice index (defaults to 2 for process.argv).\nconst parsed = nopt(knownOpts, shortHands, process.argv, 2);\n\nconsole.log('Parsed Options:', parsed);\nconsole.log('Remaining Arguments:', parsed.argv.remain);\n\n// Example usage:\n// node your-script.js --foo \"blerp\" --no-flag\n// node your-script.js --blatzk=1000 -fp","lang":"javascript","description":"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."},"warnings":[{"fix":"Ensure your project's Node.js environment meets the `^20.17.0 || >=22.9.0` requirement. Upgrade Node.js if necessary.","message":"Version `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`.","severity":"breaking","affected_versions":">=9.0.0"},{"fix":"Verify your Node.js version is `^18.17.0 || >=20.5.0`. Older versions will no longer be supported.","message":"Version `8.0.0` introduced a breaking change by raising the minimum Node.js engine compatibility to `^18.17.0 || >=20.5.0`.","severity":"breaking","affected_versions":">=8.0.0 <9.0.0"},{"fix":"Update your Node.js runtime to one within the `^14.17.0 || ^16.13.0 || >=18.0.0` range.","message":"Version `7.0.0` dropped support for older Node.js versions. The compatible range became `^14.17.0 || ^16.13.0 || >=18.0.0`.","severity":"breaking","affected_versions":">=7.0.0 <8.0.0"},{"fix":"If upgrading from `nopt@5.x` or older, ensure your Node.js version is at least 12.x LTS or 14.x LTS.","message":"Version `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.","severity":"breaking","affected_versions":">=6.0.0 <7.0.0"},{"fix":"Upgrade to `nopt@6.0.0` or newer to avoid deprecated API usage warnings.","message":"The `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.","severity":"deprecated","affected_versions":"<6.0.0"}],"env_vars":null,"search_vec":"'20.17.0':84 '22.9.0':85 '9.0.0':81 'align':72 'argument':37 'array':32 'automat':94 'battl':100 'battle-test':99 'boolean':48 'breviti':55 'cadenc':67 'capabl':93 'cli':110 'command':18,35 'command-lin':17,34 'complex':31 'configur':39 'consist':65 'core':102 'current':78 'cycl':76 'date':51 'defin':42 'definit':92 'differenti':88 'famous':23 'flexibl':11 'frequent':71 'handl':29,59 'includ':89 'integr':106 'interfac':20 'javascript':111 'key':87 'librari':6 'line':19,36 'lts':75 'maintain':63 'major':69 'mechan':57 'node':83 'node.js':1,16,74 'nopt':4,40 'npm':26,109 'number':47 'option':2,12,44,61 'pars':13 'parser':3 'path':49,95 'proven':103 'provid':7 'releas':66 'requir':82 'resolut':96 'robust':9 'shorthand':53 'solut':14 'stabl':79 'stream':52 'string':46 'strong':90 'support':41 'test':101 'type':45,91 'unknown':60 'url':50 'use':24 'various':43 'version':70,80","created_at":"2026-04-20T01:55:45.431310+00:00","updated_at":"2026-04-20T01:55:45.431310+00:00","problems":[{"fix":"Ensure 'nopt' is installed as a dependency: `npm install nopt` or `yarn add nopt`. Verify your `NODE_PATH` if using custom module paths.","cause":"The 'nopt' package is not installed or the Node.js module resolution path is incorrect.","error":"Error: Cannot find module 'nopt'"},{"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.","cause":"Attempting to import `nopt` incorrectly as a named export in an ESM context, or a corrupted `node_modules` installation.","error":"TypeError: nopt is not a function"},{"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.","cause":"The installed Node.js version does not meet the `engines` requirements specified in `nopt`'s `package.json`.","error":"The package \"nopt\" vX.Y.Z does not satisfy its \"engines\" property."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/npm/nopt","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/nopt","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}