{"record":{"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/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/server/environments/fetchableEnvironments.ts#L1-L34","documentation":"createFetchableDevEnvironment requires the global Request and Response constructors (the Fetch standard) so that its dispatchFetch/handleRequest contract can work. If either global is undefined it throws a TypeError immediately. Fetchable environments expose HTTP-style request/response handling, which is impossible without those globals.","triggerScenarios":"Calling createFetchableDevEnvironment in a runtime without fetch globals — e.g. an old Node version (< 18) where Request/Response are not global, or a stripped-down JS engine. The check runs at the top of the factory before any setup.","commonSituations":"Running Vite's fetchable environment on Node 16 or below, in a custom embedded runtime, or under a test harness that deletes fetch globals. Also when a polyfill is loaded after the environment is created.","solutions":["Run on Node 18+ (or a runtime) where Request and Response are global.","Polyfill the globals before creating the environment: install undici / whatwg-fetch and assign globalThis.Request/Response.","If you do not need the fetchable contract, use the regular DevEnvironment instead.","Ensure any polyfill module is imported before the Vite environment code runs."],"exampleFix":"// before — Node < 18 or stripped runtime\ncreateFetchableDevEnvironment('client', config, ctx)\n// after — polyfill first\nimport { Request, Response } from 'undici'\nglobalThis.Request = globalThis.Request || Request\nglobalThis.Response = globalThis.Response || Response\ncreateFetchableDevEnvironment('client', config, ctx)","handlingStrategy":"type-guard","validationCode":"function assertFetchGlobals() {\n  if (typeof Request === 'undefined' || typeof Response === 'undefined') {\n    throw new TypeError('Polyfill global Request/Response before creating a fetchable environment')\n  }\n}\nassertFetchGlobals()","typeGuard":"function hasFetchGlobals(): boolean {\n  return typeof Request !== 'undefined' && typeof Response !== 'undefined'\n}","tryCatchPattern":null,"preventionTips":["Run on Node 18+ where Request/Response are global.","Polyfill fetch globals (e.g. undici) before creating the environment.","Use the regular DevEnvironment if you do not need the fetchable contract."],"tags":["environments","fetch","runtime","node-version"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}