[PR #5559] [MERGED] feat(cli): support Cloudflare Workers virtual module imports #14331

Closed
opened 2026-04-13 09:25:18 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5559
Author: @chhoumann
Created: 10/24/2025
Status: Merged
Merged: 10/28/2025
Merged by: @Bekacru

Base: canaryHead: fix/cloudflare-workers-cli


📝 Commits (2)

  • 78a596e fix(cli): stub cloudflare modules for config loading
  • 62b4e86 Merge branch 'canary' into fix/cloudflare-workers-cli

📊 Changes

3 files changed (+113 additions, -0 deletions)

View changed files

packages/cli/src/utils/add-cloudflare-modules.ts (+83 -0)
📝 packages/cli/src/utils/get-config.ts (+2 -0)
📝 packages/cli/test/get-config.test.ts (+28 -0)

📄 Description

This fixes #5178, which broke the CLI for anyone using Better Auth with Cloudflare Workers and D1.

The Problem

When users run @better-auth/cli generate (or any CLI command) in a Cloudflare Workers project, the CLI crashes with Cannot find module 'cloudflare:workers'. This happens because their auth config or database setup imports Cloudflare's runtime-only virtual modules like cloudflare:workers or cloudflare:test, and when the CLI tries to load the config through JITI, Node's module resolver can't find them.

For Cloudflare D1 users, this completely blocks schema generation and migrations—they can't use the CLI at all.

The Fix

I added a lightweight stub module that exports the key Cloudflare Workers APIs (env, WorkerEntrypoint, DurableObject, RpcTarget, etc.) as forgiving proxies. When the CLI loads a config, it now automatically resolves cloudflare:workers and cloudflare:test to this stub through JITI's alias system, just like we already do for SvelteKit's virtual modules.

The stub uses data URIs (no temp files), requires no new dependencies, and matches Cloudflare's actual API signatures closely enough that configs load without issue. The proxies absorb property access and method calls gracefully, so even if a config references env.DB or constructs a DurableObject, everything works.

Testing

I verified this two ways:

1. Unit tests
Added a regression test in packages/cli/test/get-config.test.ts that reproduces the exact failure from #5178—a config that imports { env } from cloudflare:workers and uses it in the auth config. All 17 tests pass:

pnpm --filter @better-auth/cli exec vitest run test/get-config.test.ts
✓ test/get-config.test.ts (17 tests) 199ms

2. Real project
I tested against a local Cloudflare D1 project that was failing before. First, with the published version:

❯ bun install @better-auth/cli
installed @better-auth/cli@1.3.30

❯ bunx @better-auth/cli generate --output src/db/auth-schema1.ts
ERROR: Cannot find module 'cloudflare:workers'

Then I linked the patched version:

❯ bun link @better-auth/cli
installed @better-auth/cli@link:@better-auth/cli

❯ bunx @better-auth/cli generate --output src/db/auth-schema1.ts
✔ Do you want to generate the schema to src/db/auth-schema1.ts? … yes
SUCCESS: 🚀 Schema was generated successfully!

The fix works exactly as intended.

Closes #5178.


Summary by cubic

Fix the CLI crash in Cloudflare Workers projects by resolving Cloudflare virtual modules to a stub during config loading. Unblocks D1 schema generation and migrations (fixes #5178).

  • Bug Fixes
    • Added a stub module for cloudflare:workers and cloudflare:test (env, WorkerEntrypoint, DurableObject, RpcTarget) using data URI proxies.
    • Aliased these modules in get-config via JITI so configs load without Node resolving errors.
    • Added a regression test to verify configs importing env from cloudflare:workers load correctly.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5559 **Author:** [@chhoumann](https://github.com/chhoumann) **Created:** 10/24/2025 **Status:** ✅ Merged **Merged:** 10/28/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/cloudflare-workers-cli` --- ### 📝 Commits (2) - [`78a596e`](https://github.com/better-auth/better-auth/commit/78a596e55bf95c5296112dbf3ccca01e1e10046e) fix(cli): stub cloudflare modules for config loading - [`62b4e86`](https://github.com/better-auth/better-auth/commit/62b4e866d50569d2ead749cb7ecbbfafddb65089) Merge branch 'canary' into fix/cloudflare-workers-cli ### 📊 Changes **3 files changed** (+113 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `packages/cli/src/utils/add-cloudflare-modules.ts` (+83 -0) 📝 `packages/cli/src/utils/get-config.ts` (+2 -0) 📝 `packages/cli/test/get-config.test.ts` (+28 -0) </details> ### 📄 Description This fixes #5178, which broke the CLI for anyone using Better Auth with Cloudflare Workers and D1. ## The Problem When users run `@better-auth/cli generate` (or any CLI command) in a Cloudflare Workers project, the CLI crashes with `Cannot find module 'cloudflare:workers'`. This happens because their auth config or database setup imports Cloudflare's runtime-only virtual modules like `cloudflare:workers` or `cloudflare:test`, and when the CLI tries to load the config through JITI, Node's module resolver can't find them. For Cloudflare D1 users, this completely blocks schema generation and migrations—they can't use the CLI at all. ## The Fix I added a lightweight stub module that exports the key Cloudflare Workers APIs (`env`, `WorkerEntrypoint`, `DurableObject`, `RpcTarget`, etc.) as forgiving proxies. When the CLI loads a config, it now automatically resolves `cloudflare:workers` and `cloudflare:test` to this stub through JITI's alias system, just like we already do for SvelteKit's virtual modules. The stub uses data URIs (no temp files), requires no new dependencies, and matches Cloudflare's actual API signatures closely enough that configs load without issue. The proxies absorb property access and method calls gracefully, so even if a config references `env.DB` or constructs a `DurableObject`, everything works. ## Testing I verified this two ways: **1. Unit tests** Added a regression test in `packages/cli/test/get-config.test.ts` that reproduces the exact failure from #5178—a config that imports `{ env }` from `cloudflare:workers` and uses it in the auth config. All 17 tests pass: ```bash pnpm --filter @better-auth/cli exec vitest run test/get-config.test.ts ✓ test/get-config.test.ts (17 tests) 199ms ``` **2. Real project** I tested against a local Cloudflare D1 project that was failing before. First, with the published version: ```bash ❯ bun install @better-auth/cli installed @better-auth/cli@1.3.30 ❯ bunx @better-auth/cli generate --output src/db/auth-schema1.ts ERROR: Cannot find module 'cloudflare:workers' ``` Then I linked the patched version: ```bash ❯ bun link @better-auth/cli installed @better-auth/cli@link:@better-auth/cli ❯ bunx @better-auth/cli generate --output src/db/auth-schema1.ts ✔ Do you want to generate the schema to src/db/auth-schema1.ts? … yes SUCCESS: 🚀 Schema was generated successfully! ``` The fix works exactly as intended. Closes #5178. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix the CLI crash in Cloudflare Workers projects by resolving Cloudflare virtual modules to a stub during config loading. Unblocks D1 schema generation and migrations (fixes #5178). - **Bug Fixes** - Added a stub module for cloudflare:workers and cloudflare:test (env, WorkerEntrypoint, DurableObject, RpcTarget) using data URI proxies. - Aliased these modules in get-config via JITI so configs load without Node resolving errors. - Added a regression test to verify configs importing env from cloudflare:workers load correctly. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 09:25:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#14331