Skip to content

[BUG] dotenv fails to load backend env when auth/db run inside SvelteKit SSR. #651

Closed

Description

@az-ka

Summary

While running the SvelteKit dev server (apps/web), I call auth.api.getSession from hooks.server.ts. The @project/auth package imports @project/db, which only reads from process.env without explicitly loading the env file. Because the SSR process runs with cwd = apps/web, dotenv only sees the frontend .env, so DATABASE_URL and DATABASE_AUTH_TOKEN remain empty. As a result, the database connection fails and the dev page returns HTTP 500.

Context

Monorepo Structure:

  • Backend: apps/server
  • Frontend (SvelteKit): apps/web

Environment Variables:

  • Backend credentials (DATABASE_URL, DATABASE_AUTH_TOKEN, etc.) are stored in apps/server/.env
  • apps/web/.env only contains PUBLIC_* variables

hooks.server.ts

import { auth } from "@project/auth";
import type { Handle } from "@sveltejs/kit";
import { svelteKitHandler } from "better-auth/svelte-kit";
import { building } from "$app/environment";

export const handle: Handle = async ({ event, resolve }) => {
  const session = await auth.api.getSession({
    headers: event.request.headers,
  });
  
  if (session) {
    event.locals.session = session.session;
    event.locals.user = session.user;
  }
  
  return svelteKitHandler({ event, resolve, auth, building });
};

Terminal Output

When running bun run dev inside apps/web:

Database URL: Not set
Database Auth Token: Not set
LibsqlError: URL_INVALID: The URL '' is not in a valid format
Error: Missing API key. Pass it to the constructor `new SomeService("key")`

Visiting http://localhost:5173 returns a 500 error.

Current Workaround

Add import "dotenv/config"; to packages/db/src/index.ts, then duplicate the backend credentials (DATABASE_URL, DATABASE_AUTH_TOKEN, etc.) into apps/web/.env.

Issue: Secrets are now duplicated across two env files.

Suggested Fix

Provide a shared helper so the auth/db packages always load the backend .env even when executed inside the frontend SSR process, or document that developers must keep the env variables in apps/server/.env and apps/web/.env in sync during development.

Activity

  1. AmanVarshney01 commented on Oct 25, 2025

    @AmanVarshney01
    Owner

    Sveltekit SSR is not supported yet

    Will add this soon

  2. az-ka commented on Oct 25, 2025

    @az-ka
    Author

    Sveltekit SSR is not supported yet

    Will add this soon

    Got it, thanks for the confirmation.

    1. So, for now, is the best workaround to perform all data fetching on the client side?
    2. Once this is supported or fixed, will I be able to update my current project, or will I need to start over with a new one using bun create better-t-stack@latest?
  3. jroth1111 commented on Jan 2, 2026

    @jroth1111

    FYI: I opened a consolidated Cloudflare env injection proposal that references this issue and suggests a root .dev.vars + explicit Wrangler bindings flow: #787

  4. AmanVarshney01 commented on Oct 21, 2026

    @AmanVarshney01
    Owner

    SvelteKit runs auth and the database in its own server with --backend self (#1025), and dotenv was replaced by Varlock (#1224), so shared packages now get their env from the app. If you still see this on the latest version, please reopen or open a new issue with the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions