jackwener/OpenCLI · error · ArgumentError
npm ${label} cannot be empty
Error message
npm ${label} cannot be empty What it means
requireString validates that a labeled argument is a non-empty string after trimming; when the value is null, undefined, or whitespace-only it throws ArgumentError `npm ${label} cannot be empty`. It is the first-line guard for CLI arguments like `query` in the search command, so malformed input fails fast before any network call.
Source
Thrown at clis/npm/utils.js:14
// Shared helpers for the npm adapters that hit the public npm registry
// (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;
}
View on GitHub (pinned to 49907e53dc)
Solutions
- Pass the required argument, e.g. `npm search --query react` or `{ query: 'react' }` in code.
- Check that the shell/CI variable supplying the value is non-empty before invoking.
- Trim user input upstream and reject empty values with a clear message before calling the library.
- Wrap calls in try/catch for ArgumentError to print friendly usage text instead of a stack trace.
Example fix
// before
await npmSearch({ query: process.env.Q }); // Q unset -> ArgumentError
// after
const q = (process.env.Q ?? '').trim();
if (!q) throw new Error('Set Q to a search term, e.g. Q=react');
await npmSearch({ query: q }); Defensive patterns
Strategy: validation
Validate before calling
function ensureNonEmpty(v, label) {
const s = String(v ?? '').trim();
if (!s) throw new Error(`${label} is required`);
return s;
}
const query = ensureNonEmpty(args.query, 'query'); Type guard
function isNonEmptyString(v) {
return typeof v === 'string' && v.trim().length > 0;
} Try / catch
try {
return await npmSearch({ query });
} catch (e) {
if (e.name === 'ArgumentError') {
console.error('Usage: npm search --query <term>');
return null;
}
throw e;
} Prevention
- Trim and check required args before calling library functions.
- Avoid interpolating possibly-empty shell variables into CLI flags.
- Provide defaults in wrappers instead of passing undefined.
- Standardize an arg-validation layer shared by all CLI entry points.
When it happens
Trigger: Calling a command (e.g. `npm search`) without its required argument: args.query undefined because the flag was omitted, or passed as empty/whitespace (`--query ""`), or a programmatic caller passing null/undefined.
Common situations: Forgot `--query` on the CLI; shell variable holding the query was unset/empty (`--query "$Q"` with Q empty); empty value from a CI variable; scripting the command and forgetting the field.
Related errors
- series_id must be a non-empty value
- Invalid Chess.com username "${value}" Usernames are 3-25 cha
- coingecko derivatives limit must be a positive integer
- limit must be a positive integer
- Search keyword cannot be empty
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/cc70aad74ab5362f.
Report an issue: GitHub.