[GH-ISSUE #6545] error on cloudflare switching from 1.3.40 to 1.4.5 #27873

Closed
opened 2026-04-17 19:07:48 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @husayt on GitHub (Dec 5, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6545

Once we switched from 1.3 to 1.4.5 it was working ok locally, but when deployed to Cloudflare we had this issue

  "message": "You are using the default secret. Please set `BETTER_AUTH_SECRET` in your environment variables or pass `secret` in your auth config.",
          "statusCode": 500

I looked into list of breaking changes here, but none of them should have affected us.

we use Google oauth login and magiclinks login via magicLink plugin. Both of these methods give this error. Seems like it is not able to find environment variables anymore. Once we rolled back to 1.3 it all worked fine. So seems like some mechanism in getting env variables been affected after 1.4.5.

Posting this here, in case others have similar problem. Hopefully with more details we can figure out the resolution for this

Originally created by @husayt on GitHub (Dec 5, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6545 Once we switched from 1.3 to 1.4.5 it was working ok locally, but when deployed to Cloudflare we had this issue ``` "message": "You are using the default secret. Please set `BETTER_AUTH_SECRET` in your environment variables or pass `secret` in your auth config.", "statusCode": 500 ``` I looked into list of breaking changes [here](https://www.better-auth.com/blog/1-4), but none of them should have affected us. we use Google oauth login and magiclinks login via magicLink plugin. Both of these methods give this error. Seems like it is not able to find environment variables anymore. Once we rolled back to 1.3 it all worked fine. So seems like some mechanism in getting env variables been affected after 1.4.5. Posting this here, in case others have similar problem. Hopefully with more details we can figure out the resolution for this
GiteaMirror added the lockedbug labels 2026-04-17 19:07:48 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 5, 2025):

Tag @better-auth-agent if you would like an answer from the Agent

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3616569929 --> @better-auth-agent[bot] commented on GitHub (Dec 5, 2025): Tag @better-auth-agent if you would like an answer from the Agent _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@dosubot[bot] commented on GitHub (Dec 5, 2025):

This is a known issue after upgrading to Better Auth 1.4.x on Cloudflare. The new version changed how environment variables are accessed: it now tries to read from process.env, Deno.env, Bun.env, or globalThis.__env__, but Cloudflare Workers don’t expose process.env by default. To fix this, you need to update your Cloudflare config:

  1. In your wrangler.toml, add:

    compatibility_flags = ["nodejs_compat"]
    compatibility_date = "2024-09-23" # or a recent date
    

    This enables Node.js polyfills and makes environment variables accessible as expected by Better Auth 1.4.x. If you only need AsyncLocalStorage, you can use ["nodejs_als"] instead, but nodejs_compat is safer for most setups.
    Reference

  2. Make sure you are injecting BETTER_AUTH_SECRET as an environment variable in your Cloudflare deployment (either via the dashboard or wrangler.toml).

  3. Move your Better Auth and database initialization inside the API route handler (not at the module/global scope). This ensures env vars are available at runtime in Cloudflare’s serverless environment.
    Reference

  4. If you still have issues, you can pass the secret directly in your Better Auth config:

    betterAuth({ secret: process.env.BETTER_AUTH_SECRET })
    

    This can help if env var access is still unreliable.

If you use Vite or another bundler, exclude better-auth from dependency optimization to avoid ESM/CJS interop issues:

vite: {
  optimizeDeps: {
    exclude: ['better-auth']
  }
}

Reference

These steps should resolve the missing secret error on Cloudflare with Better Auth 1.4.x.

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:3616586909 --> @dosubot[bot] commented on GitHub (Dec 5, 2025): <!-- Answer --> This is a known issue after upgrading to Better Auth 1.4.x on Cloudflare. The new version changed how environment variables are accessed: it now tries to read from `process.env`, `Deno.env`, `Bun.env`, or `globalThis.__env__`, but Cloudflare Workers don’t expose `process.env` by default. To fix this, you need to update your Cloudflare config: 1. In your `wrangler.toml`, add: ```toml compatibility_flags = ["nodejs_compat"] compatibility_date = "2024-09-23" # or a recent date ``` This enables Node.js polyfills and makes environment variables accessible as expected by Better Auth 1.4.x. If you only need AsyncLocalStorage, you can use `["nodejs_als"]` instead, but `nodejs_compat` is safer for most setups. [Reference](https://github.com/better-auth/better-auth/blob/015a5c4b660b69f2a941ffd069ea831c8670a45d/docs/content/blogs/1-4.mdx#L388-L490) 2. Make sure you are injecting `BETTER_AUTH_SECRET` as an environment variable in your Cloudflare deployment (either via the dashboard or `wrangler.toml`). 3. Move your Better Auth and database initialization inside the API route handler (not at the module/global scope). This ensures env vars are available at runtime in Cloudflare’s serverless environment. [Reference](https://github.com/better-auth/better-auth/issues/4526) 4. If you still have issues, you can pass the secret directly in your Better Auth config: ```js betterAuth({ secret: process.env.BETTER_AUTH_SECRET }) ``` This can help if env var access is still unreliable. If you use Vite or another bundler, exclude `better-auth` from dependency optimization to avoid ESM/CJS interop issues: ```js vite: { optimizeDeps: { exclude: ['better-auth'] } } ``` [Reference](https://github.com/better-auth/better-auth/issues/4541) These steps should resolve the missing secret error on Cloudflare with Better Auth 1.4.x. <!-- 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/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8aef9a6f-4854-4263-a0c9-1fb65c6a2785?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=This%20is%20a%20known%20issue%20after%20upgrading%20to%20Better%20Auth%201.4.x%20on%20Cloudflare.%20The%20new%20version%20changed%20how%20environment%20variables%20are%20accessed%3A%20it%20now%20tries%20to%20read%20from%20%60process.env%60%2C%20%60Deno.env%60%2C%20%60Bun.env%60%2C%20or%20%60globalThis.__env__%60%2C%20but%20Cloudflare%20Workers%20don%E2%80%99t%20expose%20%60process.env%60%20by%20default.%20To%20fix%20this%2C%20you%20need%20to%20update%20your%20Cloudflare%20config%3A%0A%0A1.%20In%20your%20%60wrangler.toml%60%2C%20add%3A%0A%20%20%20%60%60%60toml%0A%20%20%20compatibility_flags%20%3D%20%5B%22nodejs_compat%22%5D%0A%20%20%20compatibility_date%20%3D%20%222024-09-23%22%20%23%20or%20a%20recent%20date%0A%20%20%20%60%60%60%0A%20%20%20This%20enables%20Node.js%20polyfills%20and%20makes%20environment%20variables%20accessible%20as%20expected%20by%20Better%20Auth%201.4.x.%20If%20you%20only%20need%20AsyncLocalStorage%2C%20you%20can%20use%20%60%5B%22nodejs_als%22%5D%60%20instead%2C%20but%20%60nodejs_compat%60%20is%20safer%20for%20most%20setups.%20%20%0A%20%20%20%5BReference%5D%28https%3A//github.com/better-auth/better-auth/blob/015a5c4b660b69f2a941ffd069ea831c8670a45d/docs/content/blogs/1-4.mdx%23L388-L490%29%0A%0A2.%20Make%20sure%20you%20are%20injecting%20%60BETTER_AUTH_SECRET%60%20as%20an%20environment%20variable%20in%20your%20Cloudflare%20deployment%20%28either%20via%20the%20dashboard%20or%20%60wrangler.toml%60%29.%0A%0A3.%20Move%20your%20Better%20Auth%20and%20database%20initialization%20inside%20the%20API%20route%20handler%20%28not%20at%20the%20module/global%20scope%29.%20This%20ensures%20env%20vars%20are%20available%20at%20runtime%20in%20Cloudflare%E2%80%99s%20serverless%20environment.%20%20%0A%20%20%20%5BReference%5D%28https%3A//github.com/better-auth/better-auth/issues/4526%29%0A%0A4.%20If%20you%20still%20have%20issues%2C%20you%20can%20pass%20the%20secret%20directly%20in%20your%20Better%20Auth%20config%3A%0A%20%20%20%60%60%60js%0A%20%20%20betterAuth%28%7B%20secret%3A%20process.env.BETTER_AUTH_SECRET%20%7D%29%0A%20%20%20%60%60%60%0A%20%20%20This%20can%20help%20if%20env%20var%20access%20is%20still%20unreliable.%0A%0AIf%20you%20use%20Vite%20or%20another%20bundler%2C%20exclude%20%60better-auth%60%20from%20dependency%20optimization%20to%20avoid%20ESM/CJS%20interop%20issues%3A%0A%60%60%60js%0Avite%3A%20%7B%0A%20%20optimizeDeps%3A%20%7B%0A%20%20%20%20exclude%3A%20%5B%27better-auth%27%5D%0A%20%20%7D%0A%7D%0A%60%60%60%0A%5BReference%5D%28https%3A//github.com/better-auth/better-auth/issues/4541%29%0A%0AThese%20steps%20should%20resolve%20the%20missing%20secret%20error%20on%20Cloudflare%20with%20Better%20Auth%201.4.x.)&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/6545)
Author
Owner

@himself65 commented on GitHub (Jan 3, 2026):

It might be because you didn't set it in the environment variables. I think it's the config issue, not in better auth side

<!-- gh-comment-id:3706909617 --> @himself65 commented on GitHub (Jan 3, 2026): It might be because you didn't set it in the environment variables. I think it's the config issue, not in better auth side
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27873