{"record":{"id":"8a5168decb9c4147","repo":"vitejs/vite","slug":"environment-name-is-not-defined-in-the-config","errorCode":null,"errorMessage":"Environment \"${name}\" is not defined in the config.","messagePattern":"Environment \"(.+?)\" is not defined in the config\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/build.ts","lineNumber":1755,"sourceCode":"export const toOutputFilePathInCss: typeof toOutputFilePathWithoutRuntime =\n  toOutputFilePathWithoutRuntime\nexport const toOutputFilePathInHtml: typeof toOutputFilePathWithoutRuntime =\n  toOutputFilePathWithoutRuntime\n\nexport class BuildEnvironment extends BaseEnvironment {\n  mode = 'build' as const\n\n  isBuilt = false\n  constructor(\n    name: string,\n    config: ResolvedConfig,\n    setup?: {\n      options?: EnvironmentOptions\n    },\n  ) {\n    let options = config.environments[name]\n    if (!options) {\n      throw new Error(`Environment \"${name}\" is not defined in the config.`)\n    }\n    if (setup?.options) {\n      options = mergeConfig(\n        options,\n        setup.options,\n      ) as ResolvedEnvironmentOptions\n    }\n    super(name, config, options)\n  }\n\n  async init(): Promise<void> {\n    if (this._initiated) {\n      return\n    }\n    this._initiated = true\n  }\n}\n","sourceCodeStart":1737,"sourceCodeEnd":1773,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/build.ts#L1737-L1773","documentation":"A BuildEnvironment instance is constructed for a named environment (client, ssr, or a custom one). The constructor reads config.environments[name]; if that key is absent it throws. This means the environment was requested for a build but never declared in vite.config.environments (nor created by the default environment backfill).","triggerScenarios":"Calling builder.build(environment) or programmatic APIs that request an environment name not present in resolvedConfig.environments. Also triggered by --mode or environment-specific build flags that reference an undeclared environment.","commonSituations":"Custom environment names declared in a plugin's config hook that runs only in serve; building with a stale config; renaming an environment key without updating references; environment-agnostic scripts that iterate names and hit a removed one.","solutions":["Declare the environment under environments in vite.config (e.g. environments: { worker: { ... } }).","Check the spelling of the environment name in CLI flags and build calls.","Ensure any plugin that strips environments in configResolved does not remove the one you are building."],"exampleFix":"// before\n// vite build --environment worker  but config has no environments.worker\n// after\nexport default defineConfig({\n  environments: { worker: { build: { rollupOptions: { input: 'src/worker.ts' } } } }\n})","handlingStrategy":"validation","validationCode":"function environmentExists(config, name) {\n  return Boolean(config && config.environments && Object.prototype.hasOwnProperty.call(config.environments, name));\n}","typeGuard":"function isKnownEnvironment(name, config) {\n  return name in (config?.environments ?? {});\n}","tryCatchPattern":null,"preventionTips":["Declare every environment you build under config.environments.","Keep environment names consistent across CLI flags, configs, and plugins."],"tags":["build","environments","config"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}