jackwener/OpenCLI · error · ArgumentError

npm package name "${value}" is not a valid registry name

Error message

npm package name "${value}" is not a valid registry name

What it means

requirePackageName validates the name against PKG_NAME — the npm name grammar (optional scoped `@scope/name`, 1-214 chars of lowercase letters, digits, '-', '.', '_') — and throws ArgumentError `is not a valid registry name` with a hint when the pattern fails. This mirrors npm's new-package naming rules, so invalid syntax never reaches the registry.

Source

Thrown at clis/npm/utils.js:25

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}`);
    }
    return n;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Fix the name to npm's grammar: `react`, `lodash`, `@scope/name` — lowercase letters, digits, '-', '.', '_' only.
  2. For legacy names with uppercase, resolve the current lowercase name via `npm view`.
  3. Strip protocol/path prefixes before passing the name.
  4. Catch ArgumentError and show the provided hint text to guide the user.

Example fix

// before
await npmPackage({ name: '@vercel' });     // invalid: scope without /name
await npmPackage({ name: 'my package' });  // invalid: space
// after
await npmPackage({ name: '@vercel/og' });
await npmPackage({ name: 'mypackage' });
Defensive patterns

Strategy: validation

Validate before calling

const PKG_NAME = /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/i;
function isValidNpmName(v) {
  const s = String(v ?? '').trim();
  return s.length >= 1 && s.length <= 214 && PKG_NAME.test(s);
}
if (!isValidNpmName(args.name)) throw new Error('Names are 1-214 chars of a-z/0-9/-._ (scoped: @scope/name)');

Type guard

function isScopedName(v) {
  return typeof v === 'string' && /^@[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*$/i.test(v.trim());
}

Try / catch

try {
  return await npmPackage({ name });
} catch (e) {
  if (e.name === 'ArgumentError' && /not a valid registry name/.test(e.message)) {
    console.error('Use npm name syntax: react, lodash, @scope/name');
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: Names with illegal characters or structure: spaces, more than one '/', a leading '.', '_' or '-', '@' not starting a scope, bare `@scope` without `/name`, URLs, semicolons, or other punctuation outside [-._].

Common situations: Passing `@vercel` alone without `/og`; passing a URL or file path; names with spaces from unquoted shell args; legacy uppercase-only names tested against new-package rules; shell-mangled names.

Related errors


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