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

  1. Pass a positive integer: `{ limit: 20 }`, or omit the argument to use the default (20).
  2. For CLI/config strings, coerce with Number.parseInt and validate before calling.
  3. Guard inputs yourself: reject n <= 0 or non-integers upstream with your own message.
  4. 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

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


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/2e1e15f777230ee3. Report an issue: GitHub.