Skip to content

feat: validate required environment variables at startup (closes #7) - #31

Merged
TusharW4ni merged 1 commit into
mainfrom
feat/env-validation
Aug 22, 2026
Merged

feat: validate required environment variables at startup (closes #7)#31
TusharW4ni merged 1 commit into
mainfrom
feat/env-validation

Conversation

@TusharW4ni

Copy link
Copy Markdown
Contributor

Problem (#7)

If a required env var is missing (e.g. a fresh clone with no .env), the app crashed with a confusing error deep inside the code — literally TypeError: Cannot read properties of undefined (reading 'replace') from server/utils/db.ts.

Fix

New server/utils/env.ts validates the required variables with Zod once, on import, and throws a clear message listing exactly what's missing:

Error: Invalid environment configuration:
  - DATABASE_URL: Invalid input: expected string, received undefined
  - BETTER_AUTH_SECRET: ...
  ...
Copy .env.example to .env and set the required values before starting the app.

db.ts and auth.ts consume the validated env, so validation runs before anything touches the database or auth.

Why a util and not a Nitro plugin

The issue suggested a server plugin, but db.ts reads DATABASE_URL at module load — earlier than any Nitro plugin runs — so a plugin would never get to report the problem (verified: the plugin approach still crashed with the old TypeError). Putting validation in a module that db.ts imports guarantees it runs first. Validation is skipped under Vitest (which runs without a real env).

Validated: DATABASE_URL, BETTER_AUTH_SECRET, BETTER_AUTH_URL, EMAIL_HOST, EMAIL_USER, EMAIL_PASS, EMAIL_FROM. UPLOAD_STORAGE_PATH is omitted (it has a safe code default).

Verification

  • pnpm build succeeds; pnpm test passes (validation skipped under Vitest).
  • Without env: built server exits fast (code 1) with the clear message above — before the old db crash.
  • With env: built server starts, GET /api/health200.

Closes #7.

🤖 Generated with Claude Code

Add server/utils/env.ts, which validates required env vars (DATABASE_URL, BETTER_AUTH_SECRET, BETTER_AUTH_URL, EMAIL_*) with Zod on import and throws a clear, human-readable error listing what's missing. db.ts and auth.ts consume the validated env, so a missing value fails the app fast at startup instead of surfacing as a confusing crash deep inside the database/auth later.

Validation lives in a shared util (not a Nitro plugin) because db.ts reads DATABASE_URL at module load, earlier than any plugin runs. Skipped under Vitest. Closes #7.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@TusharW4ni
TusharW4ni merged commit 815be66 into main Aug 22, 2026
1 check passed
@TusharW4ni
TusharW4ni deleted the feat/env-validation branch August 22, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DX: Add startup validation for required Environment Variables

1 participant