{"record":{"id":"9fedfb5236625926","repo":"upstash/context7","slug":"a-fetch-implementation-is-required","errorCode":null,"errorMessage":"A fetch implementation is required","messagePattern":"A fetch implementation is required","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/http/index.ts","lineNumber":64,"sourceCode":"  private readonly authToken?: string | AuthTokenProvider;\n  private readonly onResponse?: (metadata: Context7ResponseMetadata) => void;\n\n  public constructor(config: HttpClientConfig) {\n    this.options = {\n      cache: config.cache,\n      signal: config.signal,\n      timeout: config.timeout ?? DEFAULT_TIMEOUT,\n      keepAlive: config.keepAlive ?? true,\n    };\n    validateTimeout(this.options.timeout);\n\n    this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n    if (!isHttpUrl(this.baseUrl)) throw new Context7UrlError(this.baseUrl);\n\n    this.headers = { \"Content-Type\": \"application/json\", ...config.headers };\n    this.authToken = config.authToken;\n    if (!config.fetch && !globalThis.fetch) {\n      throw new TypeError(\"A fetch implementation is required\");\n    }\n    this.fetch = config.fetch ?? globalThis.fetch.bind(globalThis);\n    this.onResponse = config.onResponse;\n    this.retry = createRetryPolicy(config.retry);\n  }\n\n  public async request<TResult>(request: Context7Request): Promise<Context7Response<TResult>> {\n    const method = request.method ?? \"POST\";\n    const abortState = createAbortState(\n      [resolveSignal(this.options.signal), request.signal],\n      request.timeout ?? this.options.timeout\n    );\n    try {\n      if (abortState.signal?.aborted) {\n        throw abortError(abortState.signal.reason, abortState.timedOut());\n      }\n\n      const init: RequestInit = {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/sdk/src/http/index.ts#L46-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Upgrade to Node.js 18+ where global fetch is available","Pass an explicit fetch implementation: new Context7Client({ fetch: require('node-fetch') })","Import a fetch polyfill (undici, cross-fetch, whatwg-fetch) before constructing the client","In tests, provide a mock fetch via the config's fetch option instead of relying on globalThis"],"exampleFix":"// before\nconst client = new Context7Client({}); // Node 16: no global fetch\n// after\nimport fetch from 'node-fetch';\nconst client = new Context7Client({ fetch });","handlingStrategy":"fallback","validationCode":"if (!config.fetch && typeof globalThis.fetch !== 'function') {\n  throw new Error('Provide a fetch implementation (Node <18 or exotic runtime)');\n}","typeGuard":"function hasFetch(c: { fetch?: typeof fetch }): c is { fetch: typeof fetch } {\n  return typeof (c.fetch ?? globalThis.fetch) === 'function';\n}","tryCatchPattern":"try {\n  const client = new Context7Client(options);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('fetch implementation is required')) {\n    const { default: fetchPolyfill } = await import('undici');\n    return new Context7Client({ ...options, fetch: fetchPolyfill.fetch });\n  }\n  throw e;\n}","preventionTips":["Target Node 18+ where global fetch exists","In polyfilled/bundled environments, always pass fetch explicitly via config","Check the runtime before lazy-loading the client module","In tests, provide a mock fetch through config instead of mutating globalThis"],"tags":["fetch","runtime","dependency","http"],"backgroundTag":"missing-dependency","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}