{"record":{"id":"595325cc3357c2c6","repo":"sveltejs/kit","slug":"invalid-environment-variable-name-json-stringify","errorCode":null,"errorMessage":"Invalid environment variable name ${JSON.stringify(name)}","messagePattern":"Invalid environment variable name (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/kit/src/core/env.js","lineNumber":93,"sourceCode":"\tlet variables;\n\n\tconst runner = get_runner(vite, server);\n\n\t/** @type {typeof import('../runtime/app/env/server.js')} */ (\n\t\tawait runner.import(`${runtime_directory}/app/env/server.js`)\n\t).set_building();\n\n\ttry {\n\t\t({ variables } = await runner.import(file));\n\n\t\tif (!variables || typeof variables !== 'object') {\n\t\t\tthrow new Error(`${file} must export a variables object`);\n\t\t}\n\n\t\t// validate\n\t\tfor (const name of Object.keys(variables)) {\n\t\t\tif (!valid_identifier.test(name) || reserved.has(name)) {\n\t\t\t\tthrow new Error(`Invalid environment variable name ${JSON.stringify(name)}`);\n\t\t\t}\n\t\t}\n\t} catch (e) {\n\t\tconst error = /** @type {any} */ (e || {});\n\n\t\tif (\n\t\t\terror.code === 'ERR_MODULE_NOT_FOUND' &&\n\t\t\terror.message?.includes(`Cannot find module '$app`)\n\t\t) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot import \\`$app/*\\` modules other than \\`$app/env\\` inside \\`src/env\\``,\n\t\t\t\t{ cause: e }\n\t\t\t);\n\t\t}\n\n\t\tthrow error;\n\t} finally {\n\t\tawait server.close();","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/core/env.js#L75-L111","documentation":"Keys of the `variables` object exported from src/env.js must be valid JavaScript identifiers and must not collide with reserved names (like `$env` internals or build-injected names). SvelteKit throws when a name fails the identifier regex or is in the reserved set, since these become real module bindings injected into `$env/*` imports.","triggerScenarios":"Declaring an env variable key with dashes, dots, spaces, leading digits (e.g. `'MY-VAR': 'string'`), or a reserved word/name in src/env.js variables.","commonSituations":"Copy-pasting raw .env keys like `my-app-key` into the variables object; using JS reserved words like `class` or `delete`; accidental whitespace in keys.","solutions":["Rename the key to a valid identifier: uppercase letters, digits, underscores, not starting with a digit (e.g. 'MY_VAR').","Keep the actual env var name matching, renaming it in the deployment environment too.","Check the name against reserved SvelteKit/internal names if it looks like a normal identifier but still fails."],"exampleFix":"// before (src/env.js)\nexport const variables = { 'my-app-key': 'string' };\n// after\nexport const variables = { MY_APP_KEY: 'string' };","handlingStrategy":"validation","validationCode":"const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;\nconst reserved = new Set(['default', 'variables']);\nfor (const name of Object.keys(variables)) {\n  if (!valid_identifier.test(name) || reserved.has(name)) {\n    throw new Error(`Invalid env variable name: ${name}`);\n  }\n}","typeGuard":"function isValidEnvName(name) {\n  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name) && name === name.toUpperCase();\n}","tryCatchPattern":"try {\n  await build();\n} catch (e) {\n  if (String(e.message).startsWith('Invalid environment variable name')) {\n    console.error('Rename the env key to a valid UPPER_SNAKE_CASE identifier:', e.message);\n  } else throw e;\n}","preventionTips":["Use UPPER_SNAKE_CASE with underscores only for env variable names.","Never copy raw keys containing dashes or dots straight into the variables object.","Check reserved names before adding a new entry."],"tags":["env","validation","naming"],"backgroundTag":"invalid-env-declaration","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}