[GH-ISSUE #5178] generate cli doesnt work with cloudflare:workers #27503

Closed
opened 2026-04-17 18:33:28 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @luisrudge on GitHub (Oct 9, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5178

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

// database.ts

import { env } from "cloudflare:workers";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import { drizzle } from "drizzle-orm/d1";
import * as schema from "./schema";

export type Database = DrizzleD1Database<typeof schema>;

export const db: Database = drizzle(env.DB, { schema });

Current vs. Expected behavior

error log:

$ bunx @better-auth/cli@latest generate --config ../../apps/website/app/lib/auth.server.ts --output ./schema.ts -y
2025-10-09T02:13:19.454Z ERROR [Better Auth]: Couldn't read your auth config. Error: Cannot find module 'cloudflare:workers'
Require stack:
- /myappdir/packages/db/index.ts
    at Function._resolveFilename (node:internal/modules/cjs/loader:1244:15)
    at Function.resolve (node:internal/modules/helpers:146:19)
    at jitiResolve (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:148703)
    at jitiRequire (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:150290)
    at import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158307)
    at /myappdir/packages/db/index.ts:1:223
    at eval_evalModule (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:155533)
    at jitiRequire (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:150967)
    at import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158307)
    at /myappdir/apps/website/app/lib/auth.server.ts:3:25
    at async Function.import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158301)
    at async resolveConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/c12/dist/shared/c12.Bzgyhsy6.mjs:382:18)
    at async loadConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/c12/dist/shared/c12.Bzgyhsy6.mjs:169:23)
    at async getConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/@better-auth/cli/dist/index.mjs:1969:26)
    at async Command.generateAction (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/@better-auth/cli/dist/index.mjs:2705:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/myappdir/packages/db/index.ts'
  ]
}
error: script "generate-better-auth-config" exited with code 1

What version of Better Auth are you using?

1.3.27

System info

same error with this CLI


bunx @better-auth/cli info       
2025-10-09T02:15:14.663Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module 'cloudflare:workers'

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "my-monorepo-package-db";

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "sqlite", // or "pg" or "mysql"
  }),
  //... the rest of your config
});

Additional context

monorepo dir tree

root
- packages
- - db
- apps
- - website
Originally created by @luisrudge on GitHub (Oct 9, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5178 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ```ts // database.ts import { env } from "cloudflare:workers"; import type { DrizzleD1Database } from "drizzle-orm/d1"; import { drizzle } from "drizzle-orm/d1"; import * as schema from "./schema"; export type Database = DrizzleD1Database<typeof schema>; export const db: Database = drizzle(env.DB, { schema }); ``` ### Current vs. Expected behavior error log: ``` $ bunx @better-auth/cli@latest generate --config ../../apps/website/app/lib/auth.server.ts --output ./schema.ts -y 2025-10-09T02:13:19.454Z ERROR [Better Auth]: Couldn't read your auth config. Error: Cannot find module 'cloudflare:workers' Require stack: - /myappdir/packages/db/index.ts at Function._resolveFilename (node:internal/modules/cjs/loader:1244:15) at Function.resolve (node:internal/modules/helpers:146:19) at jitiResolve (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:148703) at jitiRequire (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:150290) at import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158307) at /myappdir/packages/db/index.ts:1:223 at eval_evalModule (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:155533) at jitiRequire (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:150967) at import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158307) at /myappdir/apps/website/app/lib/auth.server.ts:3:25 at async Function.import (/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/jiti/dist/jiti.cjs:1:158301) at async resolveConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/c12/dist/shared/c12.Bzgyhsy6.mjs:382:18) at async loadConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/c12/dist/shared/c12.Bzgyhsy6.mjs:169:23) at async getConfig (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/@better-auth/cli/dist/index.mjs:1969:26) at async Command.generateAction (file:///private/tmp/bunx-501-@better-auth/cli@latest/node_modules/@better-auth/cli/dist/index.mjs:2705:18) { code: 'MODULE_NOT_FOUND', requireStack: [ '/myappdir/packages/db/index.ts' ] } error: script "generate-better-auth-config" exited with code 1 ``` ### What version of Better Auth are you using? 1.3.27 ### System info ```bash same error with this CLI bunx @better-auth/cli info 2025-10-09T02:15:14.663Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module 'cloudflare:workers' ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { db } from "my-monorepo-package-db"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "sqlite", // or "pg" or "mysql" }), //... the rest of your config }); ``` ### Additional context monorepo dir tree ``` root - packages - - db - apps - - website ```
GiteaMirror added the locked label 2026-04-17 18:33:28 -05:00
Author
Owner

@luisrudge commented on GitHub (Oct 9, 2025):

maybe it's an issue with virtual packages? cloudflare:workers doesnt exist in the file system. not sure why better-auth would be trying to import/run the files.. why not extract the config options as json?

<!-- gh-comment-id:3383811727 --> @luisrudge commented on GitHub (Oct 9, 2025): maybe it's an issue with virtual packages? cloudflare:workers doesnt exist in the file system. not sure why better-auth would be trying to import/run the files.. why not extract the config options as json?
Author
Owner

@Bekacru commented on GitHub (Oct 9, 2025):

why not extract the config options as json?

We run the config file to load all the schemas added by the plugins, and the only way to read those is by executing the plugins.

That said, this is a bug and should be fixed.

<!-- gh-comment-id:3386425693 --> @Bekacru commented on GitHub (Oct 9, 2025): > why not extract the config options as json? We run the config file to load all the schemas added by the plugins, and the only way to read those is by executing the plugins. That said, this is a bug and should be fixed.
Author
Owner

@luisrudge commented on GitHub (Oct 9, 2025):

@Bekacru oh, that makes sense! My workaround for this was to create a fake-cli-db.ts file:

Image
<!-- gh-comment-id:3386615672 --> @luisrudge commented on GitHub (Oct 9, 2025): @Bekacru oh, that makes sense! My workaround for this was to create a `fake-cli-db.ts` file: <img width="1380" height="676" alt="Image" src="https://github.com/user-attachments/assets/490bf829-b2e2-4371-8ea2-3e956ef9d93e" />
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27503