jackwener/OpenCLI · warning · EmptyResultError

npm search

Error message

npm search

What it means

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.

Source

Thrown at clis/npm/search.js:30

    name: 'search',
    access: 'read',
    description: 'Search the public npm registry by keyword',
    domain: 'registry.npmjs.org',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'query', positional: true, required: true, help: 'Search keyword (e.g. "react", "graphql client")' },
        { name: 'limit', type: 'int', default: 20, help: 'Max results (1-250)' },
    ],
    columns: ['rank', 'name', 'version', 'description', 'weeklyDownloads', 'dependents', 'license', 'publisher', 'updated', 'url'],
    func: async (args) => {
        const query = requireString(args.query, 'query');
        const limit = requireBoundedInt(args.limit, 20, 250);
        const url = `${NPM_REGISTRY}/-/v1/search?text=${encodeURIComponent(query)}&size=${limit}`;
        const body = await npmFetch(url, 'npm search');
        const objects = Array.isArray(body?.objects) ? body.objects : [];
        if (!objects.length) {
            throw new EmptyResultError('npm search', `No npm packages matched "${query}".`);
        }
        return objects.slice(0, limit).map((obj, i) => {
            const pkg = obj?.package ?? {};
            const dl = obj?.downloads ?? {};
            return {
                rank: i + 1,
                name: String(pkg.name ?? ''),
                version: String(pkg.version ?? ''),
                description: String(pkg.description ?? ''),
                weeklyDownloads: dl.weekly != null ? Number(dl.weekly) : null,
                dependents: obj.dependents != null ? Number(obj.dependents) : null,
                license: String(pkg.license ?? ''),
                publisher: String(pkg.publisher?.username ?? ''),
                updated: String(obj.updated ?? '').slice(0, 10),
                url: pkg.links?.npm ? String(pkg.links.npm) : (pkg.name ? `https://www.npmjs.com/package/${pkg.name}` : ''),
            };
        });
    },

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Shorten or generalize the query — search matches name, description, and keywords; use one or two distinctive terms.
  2. Check spelling of the package name/keyword.
  3. Private/scoped packages are not in the public search index; query the registry directly by exact name instead.
  4. Catch EmptyResultError and prompt the user to refine the query rather than rendering an empty list.

Example fix

// before
await npmSearch({ query: '@mycompany/internal-build-tools-thing', limit: 20 }); // EmptyResultError
// after
try {
  return await npmSearch({ query: 'build-tools', limit: 20 });
} catch (e) {
  if (e.name === 'EmptyResultError') return []; // let UI render 'no results'
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

const q = (args.query ?? '').trim();
if (!q) throw new Error('query is required');
if (!/[a-z0-9]/i.test(q)) throw new Error('query must contain at least one alphanumeric character');

Type guard

function hasSearchResults(body) {
  return !!body && Array.isArray(body.objects) && body.objects.length > 0;
}

Try / catch

try {
  return await npmSearch({ query, limit });
} catch (e) {
  if (e.name === 'EmptyResultError') return []; // let UI render 'no results'
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/aed6c391353c02d9. Report an issue: GitHub.