jackwener/OpenCLI · error · ArgumentError

npm package name "${value}" is too long (max 214 chars)

Error message

npm package name "${value}" is too long (max 214 chars)

What it means

requirePackageName enforces the npm registry limit of 214 characters per package name and throws ArgumentError when the trimmed name exceeds it. npm itself rejects names longer than 214 chars, so the library short-circuits before hitting the network.

Source

Thrown at clis/npm/utils.js:22

export const NPM_REGISTRY = 'https://registry.npmjs.org';
export const NPM_API = 'https://api.npmjs.org';
const UA = 'opencli-npm-adapter (+https://github.com/jackwener/opencli)';

// npm package names: 1-214 chars, lowercase letters/numbers/-._ , scoped form `@scope/name`.
const PKG_NAME = /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/i;

export function requireString(value, label) {
    const s = String(value ?? '').trim();
    if (!s) throw new ArgumentError(`npm ${label} cannot be empty`);
    return s;
}

export function requirePackageName(value) {
    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}`);

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Supply just the package name, not a path or URL — `react`, not `./node_modules/react` or `https://npmjs.com/package/react`.
  2. Trim/split input; if handling a list of packages, pass them one at a time.
  3. If a name is legitimately near the limit, verify it with `npm view <name>` first.
  4. Catch ArgumentError to inform the user their input is too long.

Example fix

// before
await npmDownloads({ name: process.argv[2] }); // argv[2] was a 300-char path
// after
let name = process.argv[2];
if (name.includes('/')) name = name.split('/').pop(); // strip paths/URLs
if (name.length > 214) throw new Error('Not a package name');
await npmDownloads({ name });
Defensive patterns

Strategy: validation

Validate before calling

function isValidNameLength(v) {
  return typeof v === 'string' && v.trim().length >= 1 && v.trim().length <= 214;
}
if (!isValidNameLength(args.name)) throw new Error('Provide an npm package name (max 214 chars), not a path/URL');

Type guard

function isValidNameLength(v) {
  return typeof v === 'string' && v.trim().length >= 1 && v.trim().length <= 214;
}

Try / catch

try {
  return await npmPackage({ name });
} catch (e) {
  if (e.name === 'ArgumentError' && /too long/.test(e.message)) {
    console.error('That does not look like a package name (paths/URLs are not accepted).');
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a name longer than 214 characters — e.g. a file path or URL pasted as the package name, an accidentally repeated string, or concatenated scope+name strings built programmatically.

Common situations: Pasting a full URL or node_modules path into --name; concatenating many packages into one argument; generated names in tooling that skipped a truncation step.

Related errors


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