{"id":"e23dcd51e35c08ff","repo":"vitejs/vite","slug":"fetchabledevenvironment-requires-a-global-request","errorCode":null,"errorMessage":"FetchableDevEnvironment requires a global `Request` and `Response` object.","messagePattern":"FetchableDevEnvironment requires a global `Request` and `Response` object\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/server/environments/fetchableEnvironments.ts","lineNumber":16,"sourceCode":"import type { ResolvedConfig } from '../../config'\nimport type { DevEnvironmentContext } from '../environment'\nimport { DevEnvironment } from '../environment'\nimport type { Environment } from '../../environment'\n\nexport interface FetchableDevEnvironmentContext extends DevEnvironmentContext {\n  handleRequest(request: Request): Promise<Response> | Response\n}\n\nexport function createFetchableDevEnvironment(\n  name: string,\n  config: ResolvedConfig,\n  context: FetchableDevEnvironmentContext,\n): FetchableDevEnvironment {\n  if (typeof Request === 'undefined' || typeof Response === 'undefined') {\n    throw new TypeError(\n      'FetchableDevEnvironment requires a global `Request` and `Response` object.',\n    )\n  }\n\n  if (!context.handleRequest) {\n    throw new TypeError(\n      'FetchableDevEnvironment requires a `handleRequest` method during initialisation.',\n    )\n  }\n\n  return new FetchableDevEnvironment(name, config, context)\n}\n\nexport function isFetchableDevEnvironment(\n  environment: Environment,\n): environment is FetchableDevEnvironment {\n  return environment instanceof FetchableDevEnvironment\n}","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/server/environments/fetchableEnvironments.ts#L1-L34","documentation":"createFetchableDevEnvironment at packages/vite/src/node/server/environments/fetchableEnvironments.ts:16 requires global Request and Response (the Fetch API). On runtimes where either is undefined (old Node < 18, stripped globals, non-browser non-Node engines) it throws a TypeError before constructing anything.","triggerScenarios":"Constructing a FetchableDevEnvironment on Node < 18 (no global fetch); in a sandbox that deleted globalThis.Request/Response; in a JS engine without Fetch API polyfills; running under Jest with testEnvironment=node and no fetch polyfill.","commonSituations":"CI on an older Node image; embedded JS runtimes; test setups that do not provide whatwg fetch; SSR adapters targeting edge runtimes but developed on Node 16.","solutions":["Run on Node 18+ (global Request/Response ship there) or polyfill whatwg-fetch / undici before constructing the environment.","If you only need a plain DevEnvironment, use new DevEnvironment / the regular createServer path instead of the Fetchable variant.","Set globalThis.Request / globalThis.Response from your polyfill of choice before calling createFetchableDevEnvironment."],"exampleFix":"// before (Node 16, no globals)\nimport { createFetchableDevEnvironment } from 'vite'\nconst env = createFetchableDevEnvironment('ssr', config, ctx)\n\n// after\nimport { Request, Response } from 'undici'\nglobalThis.Request ??= Request\nglobalThis.Response ??= Response\nconst env = createFetchableDevEnvironment('ssr', config, ctx)","handlingStrategy":"type-guard","validationCode":"function assertFetchGlobals() {\n  if (typeof Request === 'undefined' || typeof Response === 'undefined') {\n    throw new Error('FetchableDevEnvironment needs Node >= 18 or a whatwg-fetch polyfill')\n  }\n}\nassertFetchGlobals()","typeGuard":"function hasFetchGlobals(): boolean {\n  return typeof Request !== 'undefined' && typeof Response !== 'undefined'\n}","tryCatchPattern":null,"preventionTips":["Run on Node 18+ for native global Request/Response.","Polyfill globalThis.Request/Response (undici, whatwg-fetch) before constructing a FetchableDevEnvironment.","Prefer the regular DevEnvironment path when fetch globals are unavailable."],"tags":["environment","fetch-api","node-version","runtime"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}