Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cloudflare-debug-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

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.
1 change: 1 addition & 0 deletions packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@astrojs/internal-helpers": "workspace:*",
"@astrojs/underscore-redirects": "workspace:*",
"@cloudflare/vite-plugin": "^1.32.3",
"obug": "^2.1.1",
"piccolore": "^0.1.3",
"tinyglobby": "^0.2.15",
"vite": "^7.3.2"
Expand Down
13 changes: 13 additions & 0 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createReadStream, existsSync, readFileSync } from 'node:fs';
import { appendFile, rename, stat } from 'node:fs/promises';
import { createInterface } from 'node:readline/promises';
import { fileURLToPath } from 'node:url';
import { removeLeadingForwardSlash } from '@astrojs/internal-helpers/path';
import { createRedirectsFromAstroRoutes, printAsRedirects } from '@astrojs/underscore-redirects';
import { cloudflare as cfVitePlugin, type PluginConfig } from '@cloudflare/vite-plugin';
Expand Down Expand Up @@ -206,12 +207,24 @@ export default function createIntegration({
globalThis.astroCloudflareOptions = cfPluginConfig;
}

// Replace the CJS `debug` package (pulled transitively by
// `micromark`, `stylus`, and many other npm packages) with an
// ESM-compatible shim backed by `obug`. The original `debug`
// references `module.exports` at the top level, which fails
// with `ReferenceError: module is not defined` when
// @cloudflare/vite-plugin loads it in the workerd runner used
// for `astro dev`, SSR and prerendering.
const debugShim = fileURLToPath(new URL('./shims/debug.js', import.meta.url));

updateConfig({
build: {
redirects: false,
},
session,
vite: {
resolve: {
alias: { debug: debugShim },
},
Comment on lines +225 to +227
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on obug's README, obug v2 is not a drop-in replacement for debug. Adding resolve here might break users' apps in unexpected ways. I'd recommend letting users do the replacement themselves, not in the @astro/cloudflare package

plugins: [
...(prerenderEnvironment === 'node' && command === 'dev'
? [createNodePrerenderPlugin()]
Expand Down
16 changes: 16 additions & 0 deletions packages/integrations/cloudflare/src/shims/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Drop-in ESM replacement for the `debug` package, used via Vite's
// `resolve.alias` in server environments that run under workerd
// (astro dev + @cloudflare/vite-plugin).
//
// The original `debug` package references `module.exports` at the top
// level of its CJS entrypoint, which throws `ReferenceError: module is
// not defined` when @cloudflare/vite-plugin loads it in the Workers
// runner. `obug` (https://www.npmjs.com/package/obug) is an ESM fork
// with the same behavior but exposes only named exports; this shim
// re-exposes a default export so consumers that do
// `import debug from "debug"` or `const debug = require("debug")`
// keep working after the alias is applied.
import { createDebug, disable, enable, enabled, namespaces } from 'obug';

export default createDebug;
export { createDebug, disable, enable, enabled, namespaces };
32 changes: 13 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading