Skip to content

DX: Add startup validation for required Environment Variables #7

@TusharW4ni

Description

@TusharW4ni

Context / Problem

The template relies on environment variables (like BETTER_AUTH_SECRET, DATABASE_URL). If a new developer forgets to copy .env.example to .env, or misses a key, the app might start but fail later with a confusing runtime error deep within the authentication logic.

Proposed Solution

Validate required environment variables immediately upon startup. If a required variable is missing, throw a clear, human-readable error, causing pnpm dev to fail fast.

Implementation Details

  1. You can use Zod in a Nuxt Server Plugin or utilize a community module like @t3-oss/env-nuxt.
  2. Example using Zod in a startup hook or module:
    import { z } from 'zod';
    
    const envSchema = z.object({
      DATABASE_URL: z.string().url(),
      BETTER_AUTH_SECRET: z.string().min(1),
      BETTER_AUTH_URL: z.string().url(),
      // ...
    });
    
    envSchema.parse(process.env); // Will throw a clear error if invalid

Acceptance Criteria

  • Starting the app without required .env variables fails immediately with a descriptive error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions