{"record":{"id":"aed6c391353c02d9","repo":"jackwener/OpenCLI","slug":"npm-search","errorCode":null,"errorMessage":"npm search","messagePattern":"npm search","errorType":"exception","errorClass":"EmptyResultError","httpStatus":null,"severity":"warning","filePath":"clis/npm/search.js","lineNumber":30,"sourceCode":"    name: 'search',\n    access: 'read',\n    description: 'Search the public npm registry by keyword',\n    domain: 'registry.npmjs.org',\n    strategy: Strategy.PUBLIC,\n    browser: false,\n    args: [\n        { name: 'query', positional: true, required: true, help: 'Search keyword (e.g. \"react\", \"graphql client\")' },\n        { name: 'limit', type: 'int', default: 20, help: 'Max results (1-250)' },\n    ],\n    columns: ['rank', 'name', 'version', 'description', 'weeklyDownloads', 'dependents', 'license', 'publisher', 'updated', 'url'],\n    func: async (args) => {\n        const query = requireString(args.query, 'query');\n        const limit = requireBoundedInt(args.limit, 20, 250);\n        const url = `${NPM_REGISTRY}/-/v1/search?text=${encodeURIComponent(query)}&size=${limit}`;\n        const body = await npmFetch(url, 'npm search');\n        const objects = Array.isArray(body?.objects) ? body.objects : [];\n        if (!objects.length) {\n            throw new EmptyResultError('npm search', `No npm packages matched \"${query}\".`);\n        }\n        return objects.slice(0, limit).map((obj, i) => {\n            const pkg = obj?.package ?? {};\n            const dl = obj?.downloads ?? {};\n            return {\n                rank: i + 1,\n                name: String(pkg.name ?? ''),\n                version: String(pkg.version ?? ''),\n                description: String(pkg.description ?? ''),\n                weeklyDownloads: dl.weekly != null ? Number(dl.weekly) : null,\n                dependents: obj.dependents != null ? Number(obj.dependents) : null,\n                license: String(pkg.license ?? ''),\n                publisher: String(pkg.publisher?.username ?? ''),\n                updated: String(obj.updated ?? '').slice(0, 10),\n                url: pkg.links?.npm ? String(pkg.links.npm) : (pkg.name ? `https://www.npmjs.com/package/${pkg.name}` : ''),\n            };\n        });\n    },","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/npm/search.js#L12-L48","documentation":"Thrown by the `npm search` command when the registry search endpoint returns 200 but `objects` is empty or missing, meaning no packages matched the search text. The library converts this into EmptyResultError so empty search results surface as an explicit error rather than a silent empty list.","triggerScenarios":"Calling search with a query string matching no packages — highly specific multi-word text, misspellings, keywords absent from all package metadata, or queries composed only of characters the search tokenizer ignores (punctuation-only strings).","commonSituations":"Searching for an internal/private package name (search covers only the public index); searching a full '@scope/name' string that is not indexed as text; over-specific queries; stale registry mirrors.","solutions":["Shorten or generalize the query — search matches name, description, and keywords; use one or two distinctive terms.","Check spelling of the package name/keyword.","Private/scoped packages are not in the public search index; query the registry directly by exact name instead.","Catch EmptyResultError and prompt the user to refine the query rather than rendering an empty list."],"exampleFix":"// before\nawait npmSearch({ query: '@mycompany/internal-build-tools-thing', limit: 20 }); // EmptyResultError\n// after\ntry {\n  return await npmSearch({ query: 'build-tools', limit: 20 });\n} catch (e) {\n  if (e.name === 'EmptyResultError') return []; // let UI render 'no results'\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"const q = (args.query ?? '').trim();\nif (!q) throw new Error('query is required');\nif (!/[a-z0-9]/i.test(q)) throw new Error('query must contain at least one alphanumeric character');","typeGuard":"function hasSearchResults(body) {\n  return !!body && Array.isArray(body.objects) && body.objects.length > 0;\n}","tryCatchPattern":"try {\n  return await npmSearch({ query, limit });\n} catch (e) {\n  if (e.name === 'EmptyResultError') return []; // let UI render 'no results'\n  throw e;\n}","preventionTips":["Use short, distinctive search terms (1-2 words).","Remember public-registry search excludes private/unpublished packages.","Fall back to a direct registry lookup by exact name when search finds nothing.","Offer query suggestions/refinement instead of dead-end empty results."],"tags":["npm","search","empty-result","query"],"backgroundTag":"empty-result-set","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}