Skip to content

Commit 49329fa

Browse files
committed
fix(cloudflare): alias debug to obug-backed shim in server environments
Fixes `ReferenceError: module is not defined` on every request in `astro dev` with the Cloudflare adapter. The CJS `debug` package is pulled transitively by `micromark`, `stylus`, and many other common npm packages; when `@cloudflare/vite-plugin` loads it in the workerd runner used for `astro dev`, SSR and prerendering, the top-level `module.exports` reference crashes because `module` is not a global there. Extends the approach from #15565 (which replaced astro core's direct `debug` usage with `obug`) to the downstream dependency graph by aliasing `debug` to an internal shim that re-exports `obug` with a default export, so consumers that do `import debug from "debug"` or `require("debug")` keep working after the alias is applied. The alias is added at the top-level `vite.resolve.alias` so it propagates to every Vite environment created by the adapter (astro, ssr, prerender).
1 parent 7959798 commit 49329fa

5 files changed

Lines changed: 48 additions & 19 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/cloudflare': patch
3+
---
4+
5+
Fixes `ReferenceError: module is not defined` on every request when running `astro dev` with the Cloudflare adapter. The error was triggered whenever a page's dependency graph pulled in the CJS `debug` package (transitively via `micromark`, `stylus`, and many other common npm packages). The adapter now aliases `debug` to an internal ESM shim backed by `obug` in the `ssr`, `astro`, and `prerender` Vite environments, so the workerd runner used by `@cloudflare/vite-plugin` no longer hits the missing `module` global.

packages/integrations/cloudflare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@astrojs/internal-helpers": "workspace:*",
4646
"@astrojs/underscore-redirects": "workspace:*",
4747
"@cloudflare/vite-plugin": "^1.32.3",
48+
"obug": "^2.1.1",
4849
"piccolore": "^0.1.3",
4950
"tinyglobby": "^0.2.15",
5051
"vite": "^7.3.2"

packages/integrations/cloudflare/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createReadStream, existsSync, readFileSync } from 'node:fs';
22
import { appendFile, rename, stat } from 'node:fs/promises';
33
import { createInterface } from 'node:readline/promises';
4+
import { fileURLToPath } from 'node:url';
45
import { removeLeadingForwardSlash } from '@astrojs/internal-helpers/path';
56
import { createRedirectsFromAstroRoutes, printAsRedirects } from '@astrojs/underscore-redirects';
67
import { cloudflare as cfVitePlugin, type PluginConfig } from '@cloudflare/vite-plugin';
@@ -206,12 +207,24 @@ export default function createIntegration({
206207
globalThis.astroCloudflareOptions = cfPluginConfig;
207208
}
208209

210+
// Replace the CJS `debug` package (pulled transitively by
211+
// `micromark`, `stylus`, and many other npm packages) with an
212+
// ESM-compatible shim backed by `obug`. The original `debug`
213+
// references `module.exports` at the top level, which fails
214+
// with `ReferenceError: module is not defined` when
215+
// @cloudflare/vite-plugin loads it in the workerd runner used
216+
// for `astro dev`, SSR and prerendering.
217+
const debugShim = fileURLToPath(new URL('./shims/debug.js', import.meta.url));
218+
209219
updateConfig({
210220
build: {
211221
redirects: false,
212222
},
213223
session,
214224
vite: {
225+
resolve: {
226+
alias: { debug: debugShim },
227+
},
215228
plugins: [
216229
...(prerenderEnvironment === 'node' && command === 'dev'
217230
? [createNodePrerenderPlugin()]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Drop-in ESM replacement for the `debug` package, used via Vite's
2+
// `resolve.alias` in server environments that run under workerd
3+
// (astro dev + @cloudflare/vite-plugin).
4+
//
5+
// The original `debug` package references `module.exports` at the top
6+
// level of its CJS entrypoint, which throws `ReferenceError: module is
7+
// not defined` when @cloudflare/vite-plugin loads it in the Workers
8+
// runner. `obug` (https://www.npmjs.com/package/obug) is an ESM fork
9+
// with the same behavior but exposes only named exports; this shim
10+
// re-exposes a default export so consumers that do
11+
// `import debug from "debug"` or `const debug = require("debug")`
12+
// keep working after the alias is applied.
13+
import { createDebug, disable, enable, enabled, namespaces } from 'obug';
14+
15+
export default createDebug;
16+
export { createDebug, disable, enable, enabled, namespaces };

pnpm-lock.yaml

Lines changed: 13 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)