upstash/context7 · error · TypeError

A fetch implementation is required

Error message

A fetch implementation is required

What it means

The HTTP client requires a fetch implementation: it uses config.fetch if provided, otherwise falls back to globalThis.fetch. If neither exists the constructor throws a TypeError immediately, since no request could ever be sent. This mostly affects runtimes without built-in fetch.

Solutions

  1. Upgrade to Node.js 18+ where global fetch is available
  2. Pass an explicit fetch implementation: new Context7Client({ fetch: require('node-fetch') })
  3. Import a fetch polyfill (undici, cross-fetch, whatwg-fetch) before constructing the client
  4. In tests, provide a mock fetch via the config's fetch option instead of relying on globalThis

Example fix

// before
const client = new Context7Client({}); // Node 16: no global fetch
// after
import fetch from 'node-fetch';
const client = new Context7Client({ fetch });
Defensive patterns

Strategy: fallback

Validate before calling

if (!config.fetch && typeof globalThis.fetch !== 'function') {
  throw new Error('Provide a fetch implementation (Node <18 or exotic runtime)');
}

Type guard

function hasFetch(c: { fetch?: typeof fetch }): c is { fetch: typeof fetch } {
  return typeof (c.fetch ?? globalThis.fetch) === 'function';
}

Try / catch

try {
  const client = new Context7Client(options);
} catch (e) {
  if (e instanceof TypeError && e.message.includes('fetch implementation is required')) {
    const { default: fetchPolyfill } = await import('undici');
    return new Context7Client({ ...options, fetch: fetchPolyfill.fetch });
  }
  throw e;
}

Prevention

When it happens

Trigger: Instantiating the client on Node < 18, in environments with global fetch disabled/removed, inside sandboxed workers or old React Native/JS runtimes where fetch is not on globalThis, and without supplying a fetch in the config.

Common situations: Running on legacy Node.js (16 or earlier) without a polyfill; test environments stubbing out globalThis.fetch; bundled code executing in an exotic runtime; upgrading the library in a codebase pinned to an old Node version.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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

Appendix: source

Thrown at packages/sdk/src/http/index.ts:64

  private readonly authToken?: string | AuthTokenProvider;
  private readonly onResponse?: (metadata: Context7ResponseMetadata) => void;

  public constructor(config: HttpClientConfig) {
    this.options = {
      cache: config.cache,
      signal: config.signal,
      timeout: config.timeout ?? DEFAULT_TIMEOUT,
      keepAlive: config.keepAlive ?? true,
    };
    validateTimeout(this.options.timeout);

    this.baseUrl = config.baseUrl.replace(/\/$/, "");
    if (!isHttpUrl(this.baseUrl)) throw new Context7UrlError(this.baseUrl);

    this.headers = { "Content-Type": "application/json", ...config.headers };
    this.authToken = config.authToken;
    if (!config.fetch && !globalThis.fetch) {
      throw new TypeError("A fetch implementation is required");
    }
    this.fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
    this.onResponse = config.onResponse;
    this.retry = createRetryPolicy(config.retry);
  }

  public async request<TResult>(request: Context7Request): Promise<Context7Response<TResult>> {
    const method = request.method ?? "POST";
    const abortState = createAbortState(
      [resolveSignal(this.options.signal), request.signal],
      request.timeout ?? this.options.timeout
    );
    try {
      if (abortState.signal?.aborted) {
        throw abortError(abortState.signal.reason, abortState.timedOut());
      }

      const init: RequestInit = {

View on GitHub (pinned to 4416fb855b)