jackwener/OpenCLI · error · ArgumentError
npm package name is required (e.g. "react", "@vercel/og")
Error message
npm package name is required (e.g. "react", "@vercel/og")
What it means
requirePackageName rejects empty or whitespace-only package names with the message `npm package name is required (e.g. "react", "@vercel/og")`. It guards the `name` argument of the downloads/package commands before any fetch happens; the message hints at both plain and scoped name forms.
Source
Thrown at clis/npm/utils.js:20
// (registry.npmjs.org) and download stats API (api.npmjs.org).
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
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`);
}View on GitHub (pinned to 49907e53dc)
Solutions
- Provide the name argument: `npm package --name react` or `npm downloads --name @vercel/og`.
- In scripts, verify the variable holding the name is non-empty before calling.
- For scoped packages include the full form `@scope/name`.
- Catch ArgumentError in your CLI wrapper to show usage help.
Example fix
// before
await npmPackage({ name: '' }); // ArgumentError
// after
const name = (process.env.PKG ?? '').trim();
if (!name) throw new Error('PKG is required, e.g. PKG=react');
await npmPackage({ name }); Defensive patterns
Strategy: validation
Validate before calling
function isPlausibleName(v) {
return typeof v === 'string' && v.trim().length > 0 && v.trim().length <= 214;
}
if (!isPlausibleName(args.name)) throw new Error('Provide an npm package name, e.g. react or @scope/name'); Type guard
function isNonEmptyName(v) {
return typeof v === 'string' && v.trim().length >= 1;
} Try / catch
try {
return await npmPackage({ name });
} catch (e) {
if (e.name === 'ArgumentError' && /name is required/.test(e.message)) {
console.error('Usage: npm package --name <pkg>');
return null;
}
throw e;
} Prevention
- Always require the --name flag explicitly in wrappers.
- Verify shell/CI variables are non-empty before interpolation.
- For scoped packages pass the full `@scope/name` form.
- Fail fast with usage help when required args are missing.
When it happens
Trigger: Calling `npm downloads` or `npm package` without a --name flag, with `--name ""`, or programmatically passing undefined/null for args.name.
Common situations: Omitting the name flag on the command line; an empty env variable interpolated as the name in scripts; refactored code that no longer forwards args.name.
Related errors
- npm ${label} cannot be empty
- npm package name "${value}" is too long (max 214 chars)
- npm package name "${value}" is not a valid registry name
- npm ${label} must be a positive integer
- npm ${label} must be <= ${maxValue}
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/cb46eaba7a96a638.
Report an issue: GitHub.