jackwener/OpenCLI · error · ArgumentError
npm ${label} must be a positive integer
Error message
npm ${label} must be a positive integer What it means
requireBoundedInt coerces its value to a number and requires a positive integer, throwing ArgumentError `npm ${label} must be a positive integer` otherwise (default label 'limit'). It guards pagination inputs like `limit` in the search command before a request is made; undefined falls back to the default, so only explicit bad values trigger this.
Source
Thrown at clis/npm/utils.js:37
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError('npm package name is required (e.g. "react", "@vercel/og")');
if (s.length > 214) {
throw new ArgumentError(`npm package name "${value}" is too long (max 214 chars)`);
}
if (!PKG_NAME.test(s)) {
throw new ArgumentError(
`npm package name "${value}" is not a valid registry name`,
'Names are 1–214 chars of lowercase a-z / 0-9 / "-._" (scoped form: "@scope/name").',
);
}
return s;
}
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
const raw = value ?? defaultValue;
const n = typeof raw === 'number' ? raw : Number(raw);
if (!Number.isInteger(n) || n <= 0) {
throw new ArgumentError(`npm ${label} must be a positive integer`);
}
if (n > maxValue) {
throw new ArgumentError(`npm ${label} must be <= ${maxValue}`);
}
return n;
}
export async function npmFetch(url, label) {
let resp;
try {
resp = await fetch(url, { headers: { 'user-agent': UA, accept: 'application/json' } });
}
catch (err) {
throw new CommandExecutionError(
`${label} request failed: ${err?.message ?? err}`,
'Check that registry.npmjs.org / api.npmjs.org are reachable from this network.',
);
}View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a positive integer: `{ limit: 20 }`, or omit the argument to use the default (20).
- For CLI/config strings, coerce with Number.parseInt and validate before calling.
- Guard inputs yourself: reject n <= 0 or non-integers upstream with your own message.
- Catch ArgumentError and re-prompt with the valid range (1..maxValue).
Example fix
// before
await npmSearch({ query: 'react', limit: '-5' }); // ArgumentError
// after
const raw = Number.parseInt(process.env.LIMIT ?? '20', 10);
const limit = Number.isInteger(raw) && raw > 0 ? raw : 20;
await npmSearch({ query: 'react', limit }); Defensive patterns
Strategy: validation
Validate before calling
function toPositiveInt(v, dflt) {
if (v == null) return dflt;
const n = typeof v === 'number' ? v : Number(v);
return Number.isInteger(n) && n > 0 ? n : dflt;
}
const limit = toPositiveInt(args.limit, 20); Type guard
function isPositiveInt(v) {
return typeof v === 'number' && Number.isInteger(v) && v > 0;
} Try / catch
try {
return await npmSearch({ query, limit });
} catch (e) {
if (e.name === 'ArgumentError' && /positive integer/.test(e.message)) {
return await npmSearch({ query, limit: 20 }); // fall back to default
}
throw e;
} Prevention
- Coerce CLI/config strings with Number.parseInt and validate before passing.
- Never pass fractional or negative limits; default is 20.
- Centralize numeric-arg parsing for all commands.
- Document valid ranges in CLI help text.
When it happens
Trigger: Passing limit as a non-integer (20.5), zero, a negative number, or a non-numeric string ('twenty', '', 'abc') — any value that Number() fails to turn into a positive integer.
Common situations: CLI flag parsed as a string containing '0' or a negative; user typo `--limit -1`; NaN-producing strings from config; fractional limits computed by division in scripts.
Related errors
- npm ${label} must be <= ${maxValue}
- limit must be <= 250 (CoinGecko per_page upper bound)
- --city must be a positive integer city ID, got ${JSON.string
- targetCount must be an integer between 1 and 100, got ${JSON
- maxScrolls must be an integer between 1 and 30, got ${JSON.s
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/2e1e15f777230ee3.
Report an issue: GitHub.