{"record":{"id":"2e4fc6d8e170b50f","repo":"vitejs/vite","slug":"module-runner-dynamic-access-of-import-meta-env","errorCode":null,"errorMessage":"[module runner] Dynamic access of \"import.meta.env\" is not supported. Please, use \"import.meta.env.${String(p)}\" instead.","messagePattern":"\\[module runner\\] Dynamic access of \"import\\.meta\\.env\" is not supported\\. Please, use \"import\\.meta\\.env\\.(.+?)\" instead\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/module-runner/createImportMeta.ts","lineNumber":8,"sourceCode":"import { isWindows } from '../shared/utils'\nimport { createImportMetaResolver } from './importMetaResolver'\nimport type { ModuleRunnerImportMeta } from './types'\nimport { posixDirname, posixPathToFileHref, toWindowsPath } from './utils'\n\nconst envProxy = new Proxy({} as any, {\n  get(_, p) {\n    throw new Error(\n      `[module runner] Dynamic access of \"import.meta.env\" is not supported. Please, use \"import.meta.env.${String(p)}\" instead.`,\n    )\n  },\n})\n\nexport function createDefaultImportMeta(\n  modulePath: string,\n): ModuleRunnerImportMeta {\n  const href = posixPathToFileHref(modulePath)\n  const filename = modulePath\n  const dirname = posixDirname(modulePath)\n  return {\n    filename: isWindows ? toWindowsPath(filename) : filename,\n    dirname: isWindows ? toWindowsPath(dirname) : dirname,\n    url: href,\n    env: envProxy,\n    resolve(_id: string, _parent?: string) {\n      throw new Error('[module runner] \"import.meta.resolve\" is not supported.')","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/module-runner/createImportMeta.ts#L1-L26","documentation":"In the module runner, `import.meta.env` is replaced with a Proxy (`envProxy`) whose `get` trap always throws. This is intentional: the runner cannot evaluate arbitrary dynamic property access because env vars must be statically known (they are inlined by Vite during transformation). The error tells you to access a specific property like `import.meta.env.MODE` so the transformer can replace it.","triggerScenarios":"Code running under the Vite module runner (e.g. SSR environment runner) that accesses `import.meta.env` dynamically: `import.meta.env[dynamicKey]`, `Object.keys(import.meta.env)`, spreading/destructuring the whole object, or `const e = import.meta.env; e[name]`.","commonSituations":"SSR or module-runner code that enumerates env vars generically (debug utilities, env-dumping libraries). Reading env via a computed key from config. Code originally written for a bundler context where `import.meta.env` is a real object.","solutions":["Replace dynamic access with a static property reference: `import.meta.env.SOME_NAME`.","If you need a dynamic lookup, map known keys explicitly: `const env = { MODE: import.meta.env.MODE, BASE_URL: import.meta.env.BASE_URL, ... }`.","Use `process.env` or `loadEnv` at config/bootstrap time for runtime-driven env access instead of `import.meta.env[variable]`.","Avoid destructuring/spreading `import.meta.env` as a whole object in runner-executed code."],"exampleFix":"// before\nconst key = process.env.WHICH\nconst val = import.meta.env[key]\n// after\nconst val = key === 'MODE' ? import.meta.env.MODE\n  : key === 'BASE_URL' ? import.meta.env.BASE_URL\n  : undefined","handlingStrategy":"validation","validationCode":"// Static analysis check: scan runner-executed source for dynamic import.meta.env access\nfunction hasDynamicImportMetaEnvAccess(src: string): boolean {\n  // matches: import.meta.env[  /  = import.meta.env  followed by [\n  return /import\\.meta\\.env\\s*\\[/.test(src) || /\\b\\w+\\s*=\\s*import\\.meta\\.env\\b[\\s\\S]*?\\1\\s*\\[/.test(src)\n}","typeGuard":null,"tryCatchPattern":"try {\n  return import.meta.env[MODE]\n} catch (e) {\n  if (e instanceof Error && /Dynamic access of \"import.meta.env\"/.test(e.message)) {\n    // fall back to statically known values\n    return undefined\n  }\n  throw e\n}","preventionTips":["Always reference import.meta.env.PROPERTY_NAME literally, never via a variable index.","Build a fixed allowlist object of the env vars you need instead of enumerating import.meta.env."],"tags":["module-runner","import-meta-env","ssr","dynamic-access"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}