{"record":{"id":"cc70aad74ab5362f","repo":"jackwener/OpenCLI","slug":"npm-label-cannot-be-empty","errorCode":null,"errorMessage":"npm ${label} cannot be empty","messagePattern":"npm (.+?) cannot be empty","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/npm/utils.js","lineNumber":14,"sourceCode":"// Shared helpers for the npm adapters that hit the public npm registry\n// (registry.npmjs.org) and download stats API (api.npmjs.org).\nimport { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\n\nexport const NPM_REGISTRY = 'https://registry.npmjs.org';\nexport const NPM_API = 'https://api.npmjs.org';\nconst UA = 'opencli-npm-adapter (+https://github.com/jackwener/opencli)';\n\n// npm package names: 1-214 chars, lowercase letters/numbers/-._ , scoped form `@scope/name`.\nconst PKG_NAME = /^(?:@[a-z0-9][a-z0-9._-]*\\/)?[a-z0-9][a-z0-9._-]*$/i;\n\nexport function requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError(`npm ${label} cannot be empty`);\n    return s;\n}\n\nexport function requirePackageName(value) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError('npm package name is required (e.g. \"react\", \"@vercel/og\")');\n    if (s.length > 214) {\n        throw new ArgumentError(`npm package name \"${value}\" is too long (max 214 chars)`);\n    }\n    if (!PKG_NAME.test(s)) {\n        throw new ArgumentError(\n            `npm package name \"${value}\" is not a valid registry name`,\n            'Names are 1–214 chars of lowercase a-z / 0-9 / \"-._\" (scoped form: \"@scope/name\").',\n        );\n    }\n    return s;\n}\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/npm/utils.js#L1-L32","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait npmSearch({ query: process.env.Q }); // Q unset -> ArgumentError\n// after\nconst q = (process.env.Q ?? '').trim();\nif (!q) throw new Error('Set Q to a search term, e.g. Q=react');\nawait npmSearch({ query: q });","handlingStrategy":"validation","validationCode":"function ensureNonEmpty(v, label) {\n  const s = String(v ?? '').trim();\n  if (!s) throw new Error(`${label} is required`);\n  return s;\n}\nconst query = ensureNonEmpty(args.query, 'query');","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  return await npmSearch({ query });\n} catch (e) {\n  if (e.name === 'ArgumentError') {\n    console.error('Usage: npm search --query <term>');\n    return null;\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["validation","argument-error","input-validation","npm"],"backgroundTag":"invalid-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}