upstash/context7 · error · Context7Error

query and libraryName are required

Error message

query and libraryName are required

What it means

SearchLibraryCommand's constructor requires both a query and a libraryName string and throws Context7Error immediately if either is falsy. This is fail-fast input validation so malformed searches never reach the network.

Solutions

  1. Pass non-empty strings for both arguments, e.g. searchLibrary('react hooks', 'react')
  2. Validate inputs before constructing the command and surface a user-friendly message
  3. If one parameter is genuinely optional in your flow, supply a sensible default instead of an empty string
  4. Check where the values originate (argv, env, form) and guard against empty/undefined at the source

Example fix

// before
await client.searchLibrary(query, name); // query or name may be ''
// after
if (!query?.trim() || !name?.trim()) throw new Error('query and libraryName are required');
await client.searchLibrary(query.trim(), name.trim());
Defensive patterns

Strategy: validation

Validate before calling

function assertSearchArgs(query, libraryName) {
  if (typeof query !== 'string' || !query.trim() || typeof libraryName !== 'string' || !libraryName.trim()) {
    throw new Error('query and libraryName must be non-empty strings');
  }
}

Type guard

function isNonEmptyString(v: unknown): v is string {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  await client.searchLibrary(query, libraryName);
} catch (e) {
  if (e instanceof Context7Error && e.message.includes('query and libraryName are required')) {
    console.error('Both --query and library name must be provided');
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling the search-library command with an empty string, undefined, or null for query or libraryName — e.g. searchLibrary('', 'react'), searchLibrary('hooks', undefined), or values read from unset variables.

Common situations: Building the query/libraryName from CLI args or form input without checking for empty values; destructuring options where a field is optional and missing; template strings left empty after interpolation; automation scripts with blank config entries.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16). Data as JSON: /api/errors/59b825f92ee696ed. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/src/commands/search-library/index.ts:15

import { Command } from "@commands/command";
import type { Library, SearchLibraryOptions } from "@commands/types";
import type { ApiSearchResponse } from "./types";
import type { Requester } from "@http";
import { Context7Error } from "@error";
import { formatLibrary, formatLibrariesAsText } from "@utils/format";

const DEFAULT_TYPE = "json";

export class SearchLibraryCommand extends Command<Library[] | string> {
  private readonly responseType: "json" | "txt";

  constructor(query: string, libraryName: string, options?: SearchLibraryOptions) {
    if (!query || !libraryName) {
      throw new Context7Error("query and libraryName are required");
    }

    const { type = DEFAULT_TYPE, ...requestOptions } = options ?? {};

    super(
      {
        method: "GET",
        query: { query, libraryName },
        ...requestOptions,
      },
      "v2/libs/search"
    );

    this.responseType = type;
  }

  public override async exec(client: Requester): Promise<Library[] | string> {
    const result = await this.requestResult<ApiSearchResponse>(client);

View on GitHub (pinned to 4416fb855b)