[GH-ISSUE #9140] Module scope auth instance breaks on Cloudflare Workers #28609

Closed
opened 2026-04-17 20:02:46 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @QexleOSS on GitHub (Apr 12, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/9140

I ran into an issue when trying to deploy my SolidStart app to Cloudflare Workers. The auth instance created at module scope was causing runtime errors about global scope restrictions.

Here's what I had that didn't work:

import { betterAuth } from 'better-auth';
import { Pool } from 'pg';

export const auth = betterAuth({
  database: new Pool({ connectionString: process.env.DATABASE_URL }),
  emailAndPassword: { enabled: true }
});

The error was something about async operations not being allowed at module scope in Cloudflare Workers. I ended up fixing it by creating the auth instance per-request instead:

export function createAuth() {
  const pool = new Pool(process.env.DATABASE_URL);
  return betterAuth({
    database: pool,
    emailAndPassword: { enabled: true }
  });
}

export async function GET({ request }) {
  const auth = createAuth();
  return auth.handler(request);
}

This works fine now, but it would be helpful if the docs mentioned this Cloudflare Workers limitation or if there was a warning about module scope instantiation.

Version: 1.6.2
Framework: SolidStart with Vinxi
Deployment: Cloudflare Workers

Originally created by @QexleOSS on GitHub (Apr 12, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/9140 I ran into an issue when trying to deploy my SolidStart app to Cloudflare Workers. The auth instance created at module scope was causing runtime errors about global scope restrictions. Here's what I had that didn't work: ```typescript import { betterAuth } from 'better-auth'; import { Pool } from 'pg'; export const auth = betterAuth({ database: new Pool({ connectionString: process.env.DATABASE_URL }), emailAndPassword: { enabled: true } }); ``` The error was something about async operations not being allowed at module scope in Cloudflare Workers. I ended up fixing it by creating the auth instance per-request instead: ```typescript export function createAuth() { const pool = new Pool(process.env.DATABASE_URL); return betterAuth({ database: pool, emailAndPassword: { enabled: true } }); } export async function GET({ request }) { const auth = createAuth(); return auth.handler(request); } ``` This works fine now, but it would be helpful if the docs mentioned this Cloudflare Workers limitation or if there was a warning about module scope instantiation. **Version:** 1.6.2 **Framework:** SolidStart with Vinxi **Deployment:** Cloudflare Workers
GiteaMirror added the needs: info label 2026-04-17 20:02:46 -05:00
Author
Owner

@bytaesu commented on GitHub (Apr 13, 2026):

Hi @QexleOSS,

I tested this with Hono and couldn't reproduce the issue. Does it still occur when using this in wrangler config?

"compatibility_flags": ["nodejs_compat"]
<!-- gh-comment-id:4233558128 --> @bytaesu commented on GitHub (Apr 13, 2026): Hi @QexleOSS, I tested this with Hono and couldn't reproduce the issue. Does it still occur when using this in wrangler config? ```json "compatibility_flags": ["nodejs_compat"] ```
Author
Owner

@bytaesu commented on GitHub (Apr 13, 2026):

auth.ts

Image

/get-session

Image
<!-- gh-comment-id:4233564610 --> @bytaesu commented on GitHub (Apr 13, 2026): ### `auth.ts` <img width="500" alt="Image" src="https://github.com/user-attachments/assets/934c3e23-bf18-4f33-a8ab-e81abbd4df12" /> ### `/get-session` <img width="500" alt="Image" src="https://github.com/user-attachments/assets/dbcdc16b-f6ad-46be-b838-01cbee3e6c30" />
Author
Owner

@QexleOSS commented on GitHub (Apr 13, 2026):

@bytaesu Yes. Thank you for the followup auth.ts example, I'll try it out but the issue is possibly with solidstart-vinxi since it worked properly for your case.

<!-- gh-comment-id:4240365259 --> @QexleOSS commented on GitHub (Apr 13, 2026): @bytaesu Yes. Thank you for the followup auth.ts example, I'll try it out but the issue is possibly with solidstart-vinxi since it worked properly for your case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#28609