{"id":"0528177ad68fdb7c","repo":"vitejs/vite","slug":"invalid-environment-name-name-environment-na","errorCode":null,"errorMessage":"Invalid environment name \"${name}\". Environment names must only contain alphanumeric characters and \"$\", \"_\".","messagePattern":"Invalid environment name \"(.+?)\"\\. Environment names must only contain alphanumeric characters and \"\\$\", \"_\"\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/baseEnvironment.ts","lineNumber":40,"sourceCode":"\n  /**\n   * @internal\n   */\n  _options: ResolvedEnvironmentOptions\n  /**\n   * @internal\n   */\n  _topLevelConfig: ResolvedConfig\n\n  constructor(\n    name: string,\n    topLevelConfig: ResolvedConfig,\n    options: ResolvedEnvironmentOptions = topLevelConfig.environments[name],\n  ) {\n    // only allow some characters so that we can use name without escaping for directory names\n    // and make users easier to access with `environments.*`\n    if (!/^[\\w$]+$/.test(name)) {\n      throw new Error(\n        `Invalid environment name \"${name}\". Environment names must only contain alphanumeric characters and \"$\", \"_\".`,\n      )\n    }\n    this.name = name\n    this._topLevelConfig = topLevelConfig\n    this._options = options\n    this.config = new Proxy(\n      options as ResolvedConfig & ResolvedEnvironmentOptions,\n      {\n        get: (target, prop: keyof ResolvedConfig) => {\n          if (prop === 'logger') {\n            return this.logger\n          }\n          if (prop in target) {\n            return this._options[prop as keyof ResolvedEnvironmentOptions]\n          }\n          return this._topLevelConfig[prop]\n        },","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/baseEnvironment.ts#L22-L58","documentation":"Vite validates environment names against /^\\w+$/ in the PartialEnvironment constructor (baseEnvironment.ts:39). Names are restricted to alphanumerics, underscore, and dollar sign so they can be used unescaped as directory names and accessed safely via environments.*. Any other character (hyphen, dot, space, slash) is rejected when the environment is constructed.","triggerScenarios":"Registering or constructing an environment (BaseEnvironment/PartialEnvironment, or via the environments config map) with a name like 'my-env', 'ssr.client', 'client v2', or 'ssr/worker'.","commonSituations":"Naming a custom environment in vite.config under environments: { 'my-app': {...} }; passing a hyphenated or dotted key when programmatically creating a dev/build environment; deriving the name from an OS variable that contains slashes.","solutions":["Rename the environment to use only [A-Za-z0-9_$], e.g. 'myApp' or 'ssr_client'.","If the name is derived dynamically, sanitize it against /^\\w+$/ before use.","Audit config keys and plugin code that constructs environments for stray punctuation."],"exampleFix":"// before\nenvironments: { 'ssr-client': { ... } }\n// after\nenvironments: { ssrClient: { ... } }","handlingStrategy":"validation","validationCode":"const ENV_NAME_RE = /^[\\w$]+$/\nfunction safeEnvName(name: string): string {\n  if (!ENV_NAME_RE.test(name)) {\n    throw new Error(`Invalid environment name: ${name}`)\n  }\n  return name\n}","typeGuard":"function isValidEnvironmentName(name: string): boolean {\n  return /^[\\w$]+$/.test(name)\n}","tryCatchPattern":null,"preventionTips":["Use camelCase or snake_case for environment keys (no hyphens/dots/slashes).","When deriving environment names from env vars or paths, sanitize against /^[\\w$]+$/ first.","Document allowed environment names for contributors."],"tags":["environment","config","validation","naming"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}