[GH-ISSUE #8061] SvelteKit build fails based on environment variables #19616

Closed
opened 2026-04-15 18:53:40 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @rosano on GitHub (Feb 19, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8061

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

run vite build for SvelteKit app without setting environment variables or inside a Dockerfile

Current vs. Expected behavior

Build fails with the following error:

2026-02-19T07:31:36.197Z WARN [Better Auth]: [better-auth] Base URL could not be determined. Please set a valid base URL using the baseURL config option or the BETTER_AUTH_BASE_URL environment variable. Without this, callbacks and redirects may not work correctly.

node:internal/event_target:1105
  process.nextTick(() => { throw err; });
                           ^

Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:333:10)
    at [kOnMessage] (node:internal/worker:344:37)
    at MessagePort.<anonymous> (node:internal/worker:233:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:831:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
  name: 'BetterAuthError'
}

What version of Better Auth are you using?

1.4.18

System info

error: unknown command 'info'


macOS 12.7.6, nodejs 20.19.5

Which area(s) are affected? (Select all that apply)

Package

Auth config (if applicable)


Additional context

Seems like the environment variables should be set at runtime, not build time, but an error gets thrown by these lines when building:

https://github.com/better-auth/better-auth/blob/8fe651400a2cd03c427874beb55f74446ec6a9e6/packages/better-auth/src/context/create-context.ts#L56-L60

Originally created by @rosano on GitHub (Feb 19, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8061 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce run `vite build` for SvelteKit app without setting environment variables or inside a Dockerfile ### Current vs. Expected behavior Build fails with the following error: ``` 2026-02-19T07:31:36.197Z WARN [Better Auth]: [better-auth] Base URL could not be determined. Please set a valid base URL using the baseURL config option or the BETTER_AUTH_BASE_URL environment variable. Without this, callbacks and redirects may not work correctly. node:internal/event_target:1105 process.nextTick(() => { throw err; }); ^ Emitted 'error' event on Worker instance at: at [kOnErrorMessage] (node:internal/worker:333:10) at [kOnMessage] (node:internal/worker:344:37) at MessagePort.<anonymous> (node:internal/worker:233:57) at [nodejs.internal.kHybridDispatch] (node:internal/event_target:831:20) at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) { name: 'BetterAuthError' } ``` ### What version of Better Auth are you using? 1.4.18 ### System info ```bash error: unknown command 'info' macOS 12.7.6, nodejs 20.19.5 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript ``` ### Additional context Seems like the environment variables should be set at runtime, not build time, but an error gets thrown by these lines when building: https://github.com/better-auth/better-auth/blob/8fe651400a2cd03c427874beb55f74446ec6a9e6/packages/better-auth/src/context/create-context.ts#L56-L60
GiteaMirror added the locked label 2026-04-15 18:53:40 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 19, 2026):

A couple of things to note here:

1. Wrong environment variable name: The warning message mentions BETTER_AUTH_BASE_URL, but the actual environment variable is BETTER_AUTH_URL. This naming mismatch has been reported as confusing.

2. SvelteKit building flag: For SvelteKit specifically, make sure you're passing the building flag to the handler in your hooks.server.ts:

import { building } from "$app/environment";

export async function handle({ event, resolve }) {
  return svelteKitHandler({ event, resolve, auth, building });
}

This tells Better Auth to skip auth logic during build, preventing the error when env vars aren't available.

3. For Docker/CI builds, you can also set a fallback in your config:

export const auth = betterAuth({
  baseURL: process.env.BETTER_AUTH_URL || "http://localhost:3000",
  // ...
});

The building flag is likely the most appropriate fix for your SvelteKit use case.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3925196068 --> @dosubot[bot] commented on GitHub (Feb 19, 2026): <!-- Answer --> A couple of things to note here: **1. Wrong environment variable name**: The warning message mentions `BETTER_AUTH_BASE_URL`, but the [actual environment variable is `BETTER_AUTH_URL`](https://better-auth.com/docs/reference/options). This naming mismatch [has been reported as confusing](https://github.com/better-auth/better-auth/issues/7748#issuecomment-3830956156). **2. SvelteKit `building` flag**: For SvelteKit specifically, make sure you're passing the `building` flag to the handler in your `hooks.server.ts`: ```typescript import { building } from "$app/environment"; export async function handle({ event, resolve }) { return svelteKitHandler({ event, resolve, auth, building }); } ``` This [tells Better Auth to skip auth logic during build](https://better-auth.com/docs/integrations/svelte-kit), preventing the error when env vars aren't available. **3. For Docker/CI builds**, you can also set a fallback in your config: ```typescript export const auth = betterAuth({ baseURL: process.env.BETTER_AUTH_URL || "http://localhost:3000", // ... }); ``` The `building` flag is likely the most appropriate fix for your SvelteKit use case. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/20cc8484-ec5e-4674-a768-fe58580712d8?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=A%20couple%20of%20things%20to%20note%20here%3A%0A%0A%2A%2A1.%20Wrong%20environment%20variable%20name%2A%2A%3A%20The%20warning%20message%20mentions%20%60BETTER_AUTH_BASE_URL%60%2C%20but%20the%20%5Bactual%20environment%20variable%20is%20%60BETTER_AUTH_URL%60%5D%28https%3A//better-auth.com/docs/reference/options%29.%20This%20naming%20mismatch%20%5Bhas%20been%20reported%20as%20confusing%5D%28https%3A//github.com/better-auth/better-auth/issues/7748%23issuecomment-3830956156%29.%0A%0A%2A%2A2.%20SvelteKit%20%60building%60%20flag%2A%2A%3A%20For%20SvelteKit%20specifically%2C%20make%20sure%20you%27re%20passing%20the%20%60building%60%20flag%20to%20the%20handler%20in%20your%20%60hooks.server.ts%60%3A%0A%0A%60%60%60typescript%0Aimport%20%7B%20building%20%7D%20from%20%22%24app/environment%22%3B%0A%0Aexport%20async%20function%20handle%28%7B%20event%2C%20resolve%20%7D%29%20%7B%0A%20%20return%20svelteKitHandler%28%7B%20event%2C%20resolve%2C%20auth%2C%20building%20%7D%29%3B%0A%7D%0A%60%60%60%0A%0AThis%20%5Btells%20Better%20Auth%20to%20skip%20auth%20logic%20during%20build%5D%28https%3A//better-auth.com/docs/integrations/svelte-kit%29%2C%20preventing%20the%20error%20when%20env%20vars%20aren%27t%20available.%0A%0A%2A%2A3.%20For%20Docker/CI%20builds%2A%2A%2C%20you%20can%20also%20set%20a%20fallback%20in%20your%20config%3A%0A%0A%60%60%60typescript%0Aexport%20const%20auth%20%3D%20betterAuth%28%7B%0A%20%20baseURL%3A%20process.env.BETTER_AUTH_URL%20%7C%7C%20%22http%3A//localhost%3A3000%22%2C%0A%20%20//%20...%0A%7D%29%3B%0A%60%60%60%0A%0AThe%20%60building%60%20flag%20is%20likely%20the%20most%20appropriate%20fix%20for%20your%20SvelteKit%20use%20case.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/8061)
Author
Owner

@AlexsandroBezerra commented on GitHub (Feb 20, 2026):

Same here. Looks like It is ignoring that's in build time, and runs like in production.

<!-- gh-comment-id:3931205063 --> @AlexsandroBezerra commented on GitHub (Feb 20, 2026): Same here. Looks like It is ignoring that's in build time, and runs like in production.
Author
Owner

@himself65 commented on GitHub (Feb 20, 2026):

Have you provided the secret and the base URL during the prod build?

<!-- gh-comment-id:3934502709 --> @himself65 commented on GitHub (Feb 20, 2026): Have you provided the secret and the base URL during the prod build?
Author
Owner

@rosano commented on GitHub (Feb 20, 2026):

Have you provided the secret and the base URL during the prod build?

I have not because my app is built as part of a Dockerfile to be deployed as a Node.js server-side app and unless I've mistaken it seems that the secret and base URL should come from runtime environment variables in this case. I think better-auth assumes it's production when building whereas it might fit this case better to determine the environment at runtine.

<!-- gh-comment-id:3934727484 --> @rosano commented on GitHub (Feb 20, 2026): > Have you provided the secret and the base URL during the prod build? I have not because my app is built as part of a Dockerfile to be deployed as a Node.js server-side app and unless I've mistaken it seems that the secret and base URL should come from runtime environment variables in this case. I think better-auth assumes it's production when building whereas it might fit this case better to determine the environment at runtine.
Author
Owner

@rosano commented on GitHub (Feb 21, 2026):

Using the fallback values as described by dosu.

<!-- gh-comment-id:3938264082 --> @rosano commented on GitHub (Feb 21, 2026): Using the fallback values as described by dosu.
Author
Owner

@megla-hburdow commented on GitHub (Feb 24, 2026):

This seems more like a workaround than a fix to me.

Setting fallback values will remove the secret validation also for real production cases.
Shouldn't we change the "SvelteKit Integration" to disable the eager validation on build time?

<!-- gh-comment-id:3952249518 --> @megla-hburdow commented on GitHub (Feb 24, 2026): This seems more like a workaround than a fix to me. Setting fallback values will remove the secret validation also for real production cases. Shouldn't we change the "SvelteKit Integration" to disable the eager validation on build time?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19616