{"record":{"id":"cb46eaba7a96a638","repo":"jackwener/OpenCLI","slug":"npm-package-name-is-required-e-g-react-verc","errorCode":null,"errorMessage":"npm package name is required (e.g. \"react\", \"@vercel/og\")","messagePattern":"npm package name is required \\(e\\.g\\. \"react\", \"@vercel/og\"\\)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/npm/utils.js","lineNumber":20,"sourceCode":"// (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\nexport function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {\n    const raw = value ?? defaultValue;\n    const n = typeof raw === 'number' ? raw : Number(raw);\n    if (!Number.isInteger(n) || n <= 0) {\n        throw new ArgumentError(`npm ${label} must be a positive integer`);\n    }","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/npm/utils.js#L2-L38","documentation":"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.","triggerScenarios":"Calling `npm downloads` or `npm package` without a --name flag, with `--name \"\"`, or programmatically passing undefined/null for args.name.","commonSituations":"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.","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."],"exampleFix":"// before\nawait npmPackage({ name: '' }); // ArgumentError\n// after\nconst name = (process.env.PKG ?? '').trim();\nif (!name) throw new Error('PKG is required, e.g. PKG=react');\nawait npmPackage({ name });","handlingStrategy":"validation","validationCode":"function isPlausibleName(v) {\n  return typeof v === 'string' && v.trim().length > 0 && v.trim().length <= 214;\n}\nif (!isPlausibleName(args.name)) throw new Error('Provide an npm package name, e.g. react or @scope/name');","typeGuard":"function isNonEmptyName(v) {\n  return typeof v === 'string' && v.trim().length >= 1;\n}","tryCatchPattern":"try {\n  return await npmPackage({ name });\n} catch (e) {\n  if (e.name === 'ArgumentError' && /name is required/.test(e.message)) {\n    console.error('Usage: npm package --name <pkg>');\n    return null;\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["validation","argument-error","npm","package-name"],"backgroundTag":"invalid-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}