{"record":{"id":"d2e9933e0db14278","repo":"toeverything/AFFiNE","slug":"invalid-value-value-for-environment-variable","errorCode":null,"errorMessage":"Invalid value \"${value}\" for environment variable ${env}, expected one of ${JSON.stringify(availableValues)}","messagePattern":"Invalid value \"(.+?)\" for environment variable (.+?), expected one of (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/backend/server/src/env.ts","lineNumber":80,"sourceCode":"  NAMESPACE: Namespace;\n  DEPLOYMENT_TYPE: DeploymentType;\n  version: string;\n};\n\nglobalThis.CLS_REQUEST_HOST = 'CLS_REQUEST_HOST';\nglobalThis.CUSTOM_CONFIG_PATH = join(homedir(), '.affine/config');\nglobalThis.readEnv = function readEnv<T>(\n  env: string,\n  defaultValue: T,\n  availableValues?: T[]\n) {\n  const value = process.env[env];\n  if (value === undefined) {\n    return defaultValue;\n  }\n\n  if (availableValues && !availableValues.includes(value as any)) {\n    throw new Error(\n      `Invalid value \"${value}\" for environment variable ${env}, expected one of ${JSON.stringify(\n        availableValues\n      )}`\n    );\n  }\n\n  return value as T;\n};\n\nexport class Env implements AppEnv {\n  NODE_ENV = (process.env.NODE_ENV ?? NodeEnv.Production) as NodeEnv;\n  NAMESPACE = readEnv(\n    'AFFINE_ENV',\n    Namespace.Production,\n    Object.values(Namespace)\n  );\n  DEPLOYMENT_TYPE = readEnv(\n    'DEPLOYMENT_TYPE',","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/591f874dad30887a80143a061a44bd3ca7ee3299/packages/backend/server/src/env.ts#L62-L98","documentation":"Thrown by the global readEnv(env, defaultValue, availableValues) helper during Env construction when an environment variable restricted to an enum is set to a value outside the allowed list. Validated variables include AFFINE_ENV (dev|beta|production), DEPLOYMENT_TYPE (affine|selfhosted), and SERVER_FLAVOR (allinone|graphql|sync|renderer|front|doc|script).","triggerScenarios":"Starting the server with e.g. AFFINE_ENV=staging, DEPLOYMENT_TYPE=cloud, or SERVER_FLAVOR=api — any value not in that variable's enum.","commonSituations":"Copying docker-compose/.env from other projects that use different conventions (staging instead of beta); typos; using a value valid in an older/newer AFFiNE version after upgrading.","solutions":["Set the variable to one of the values printed in the error's 'expected one of' list, e.g. AFFINE_ENV=beta (not staging).","Unset the variable if you want the default instead of guessing a value.","Check src/env.ts enums (Namespace, DeploymentType, Flavor) for your server version to see the accepted values."],"exampleFix":"# before\nAFFINE_ENV=staging\n# after\nAFFINE_ENV=beta","handlingStrategy":"validation","validationCode":"// Fail fast with a friendly message before booting the app\nconst ALLOWED = {\n  AFFINE_ENV: ['dev', 'beta', 'production'],\n  DEPLOYMENT_TYPE: ['affine', 'selfhosted'],\n  SERVER_FLAVOR: ['allinone', 'graphql', 'sync', 'renderer', 'front', 'doc', 'script'],\n};\nfor (const [key, values] of Object.entries(ALLOWED)) {\n  const v = process.env[key];\n  if (v && !values.includes(v)) {\n    console.error(`${key}=${v} invalid; expected one of ${values.join('|')}`);\n    process.exit(1);\n  }\n}","typeGuard":"const isValidAffineEnv = (value: string): boolean =>\n  ['dev', 'beta', 'production'].includes(value);","tryCatchPattern":null,"preventionTips":["Keep one canonical .env / docker-compose file per environment and validate enumed variables in a preflight script.","Remember stage names live in AFFINE_ENV (dev/beta/production), not in custom values like staging."],"tags":["environment","env-var","startup","configuration"],"backgroundTag":"invalid-env-var-value","analyzedSha":"591f874dad30887a80143a061a44bd3ca7ee3299","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}