better-auth/svelte-kit on cloudflare throws 'Error: Can only read the current request event inside functions invoked during handle, such as server load functions, actions, endpoints, and other server hooks. In environments without `AsyncLocalSt...' #2149

Closed
opened 2026-03-13 09:30:29 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @fbehrens on GitHub (Oct 19, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

-> https://github.com/fbehrens/bauth

Current vs. Expected behavior

https://bauth.fbehrens.workers.dev throws 500 - Internal error

$ wrangler tail 

 ⛅️ wrangler 4.43.0
───────────────────
Successfully created tail, expires at 2025-10-19T15:53:03Z
Connected to bauth, waiting for logs...
GET https://bauth.fbehrens.workers.dev/ - Ok @ 10/19/2025, 11:53:12 AM
  (error) 
[500] GET /
Error: Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks. In environments without `AsyncLocalStorage`, the event must be read synchronously, not after an `await`.
    at getRequestEvent (worker.js:1383:11)
    at Object.database (worker.js:67305:23)
    at getAdapter (worker.js:57940:21)
    at init (worker.js:59861:29)
    at betterAuth (worker.js:59986:27)
    at .svelte-kit/output/server/chunks/hooks.server.js (worker.js:67303:12)
    at __init (worker.js:5:56)
    at worker.js:69396:124
    at async get_hooks (worker.js:69396:88)
    at async worker.js:73949:24

What version of Better Auth are you using?

1.3.28

System info

pnpm dlx @better-auth/cli info 
2025-10-19T10:10:44.425Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module '$app/environment'
Require stack:
- /Users/fb/kata/bauth/src/lib/server/db/index.ts
    at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at jitiResolve (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:148703)
    at jitiRequire (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:150290)
    at import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158307)
    at /Users/fb/kata/bauth/src/lib/server/db/index.ts:3:26
    at async import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158301)
    at async /Users/fb/kata/bauth/src/lib/auth.ts:3:11
    at async Function.import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158301)
    at async resolveConfig (file:///Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/c12@3.3.1/node_modules/c12/dist/shared/c12.8GPsgFQh.mjs:381:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/fb/kata/bauth/src/lib/server/db/index.ts' ]
}

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

Other

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

I am currently finding my stack and this is my try on using
sveltekit with betterauth on clouldflare
The auth is working in node environment, but not on cloudflare.
As i have not much experience with cloudflare, i could have screwed sth else,
but the error I get from cloudflare, made me reporting it here ...
Thanks Frank

Originally created by @fbehrens on GitHub (Oct 19, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce -> https://github.com/fbehrens/bauth ### Current vs. Expected behavior https://bauth.fbehrens.workers.dev throws 500 - Internal error ``` $ wrangler tail ⛅️ wrangler 4.43.0 ─────────────────── Successfully created tail, expires at 2025-10-19T15:53:03Z Connected to bauth, waiting for logs... GET https://bauth.fbehrens.workers.dev/ - Ok @ 10/19/2025, 11:53:12 AM (error) [500] GET / Error: Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks. In environments without `AsyncLocalStorage`, the event must be read synchronously, not after an `await`. at getRequestEvent (worker.js:1383:11) at Object.database (worker.js:67305:23) at getAdapter (worker.js:57940:21) at init (worker.js:59861:29) at betterAuth (worker.js:59986:27) at .svelte-kit/output/server/chunks/hooks.server.js (worker.js:67303:12) at __init (worker.js:5:56) at worker.js:69396:124 at async get_hooks (worker.js:69396:88) at async worker.js:73949:24 ``` ### What version of Better Auth are you using? 1.3.28 ### System info ```bash pnpm dlx @better-auth/cli info 2025-10-19T10:10:44.425Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module '$app/environment' Require stack: - /Users/fb/kata/bauth/src/lib/server/db/index.ts at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15) at Function.resolve (node:internal/modules/helpers:145:19) at jitiResolve (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:148703) at jitiRequire (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:150290) at import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158307) at /Users/fb/kata/bauth/src/lib/server/db/index.ts:3:26 at async import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158301) at async /Users/fb/kata/bauth/src/lib/auth.ts:3:11 at async Function.import (/Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/dist/jiti.cjs:1:158301) at async resolveConfig (file:///Users/fb/Library/Caches/pnpm/dlx/422627e9d999c4228c3e0521cbdc82f4dff9ad4dcc147e44a4dbe3240e875045/199fbf1c436-b6eb/node_modules/.pnpm/c12@3.3.1/node_modules/c12/dist/shared/c12.8GPsgFQh.mjs:381:18) { code: 'MODULE_NOT_FOUND', requireStack: [ '/Users/fb/kata/bauth/src/lib/server/db/index.ts' ] } ``` ### Which area(s) are affected? (Select all that apply) Other ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context I am currently finding my stack and this is my try on using sveltekit with betterauth on clouldflare The auth is working in node environment, but not on cloudflare. As i have not much experience with cloudflare, i could have screwed sth else, but the error I get from cloudflare, made me reporting it here ... Thanks Frank
GiteaMirror added the svelte label 2026-03-13 09:30:30 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 19, 2025):

Hi @fbehrens! I'm Dosu and I’m helping the better-auth team.

This is a known limitation: better-auth relies on Node.js features like AsyncLocalStorage to track request context, which Cloudflare Workers do not support. The error occurs when request context is accessed asynchronously or outside of server hooks, which is not possible in Cloudflare's environment. This is widely reported and documented in several issues and recent code changes (example, example, example).

Workarounds include instantiating the better-auth client per request inside the handler, so it has synchronous access to the request context. This pattern works in frameworks like Hono and Remix, but breaks CLI features and type inference. For SvelteKit, you would need to avoid exporting a singleton auth instance and instead create it inside each request handler, passing in any needed environment variables or bindings (example workaround).

You may also need to switch to Cloudflare-compatible database adapters (like NeonDB with Kysely or D1) and avoid Node.js-only libraries. Some recent PRs have improved environment detection and dynamic imports, but full compatibility is not guaranteed yet (PR #5008).

In summary: better-auth is not fully compatible with Cloudflare Workers out of the box. Dynamic instantiation per request is required, but this comes with trade-offs. If you need CLI features, you may need to manage schema manually or use a Node environment for migrations.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Oct 19, 2025): <!-- Greeting --> Hi @fbehrens! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known limitation: better-auth relies on Node.js features like AsyncLocalStorage to track request context, which Cloudflare Workers do not support. The error occurs when request context is accessed asynchronously or outside of server hooks, which is not possible in Cloudflare's environment. This is widely reported and documented in several issues and recent code changes ([example](https://github.com/better-auth/better-auth/issues/1143#issuecomment-2603802804), [example](https://github.com/better-auth/better-auth/issues/969#issuecomment-3235069660), [example](https://github.com/better-auth/better-auth/pull/5008)). Workarounds include instantiating the better-auth client per request inside the handler, so it has synchronous access to the request context. This pattern works in frameworks like Hono and Remix, but breaks CLI features and type inference. For SvelteKit, you would need to avoid exporting a singleton auth instance and instead create it inside each request handler, passing in any needed environment variables or bindings ([example workaround](https://github.com/better-auth/better-auth/issues/1143#issuecomment-2603802804)). You may also need to switch to Cloudflare-compatible database adapters (like NeonDB with Kysely or D1) and avoid Node.js-only libraries. Some recent PRs have improved environment detection and dynamic imports, but full compatibility is not guaranteed yet ([PR #5008](https://github.com/better-auth/better-auth/pull/5008)). In summary: better-auth is not fully compatible with Cloudflare Workers out of the box. Dynamic instantiation per request is required, but this comes with trade-offs. If you need CLI features, you may need to manage schema manually or use a Node environment for migrations. <!-- 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/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/24910447-905c-4a3b-95f2-17817e5dfa3d?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&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/5413)
Author
Owner

@fbehrens commented on GitHub (Oct 19, 2025):

Thanks for great information

@fbehrens commented on GitHub (Oct 19, 2025): Thanks for great information
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2149