fix(cloudflare): alias debug to obug-backed shim in server environments#16569
fix(cloudflare): alias debug to obug-backed shim in server environments#16569nathanredblur wants to merge 1 commit into
debug to obug-backed shim in server environments#16569Conversation
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).
🦋 Changeset detectedLatest commit: 49329fa The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| resolve: { | ||
| alias: { debug: debugShim }, | ||
| }, |
There was a problem hiding this comment.
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
|
Hopefully, we can persuade the upstream |
I was unable to reproduce the issue. Additionally, as @ocavue noted in their review, I don't believe this is a safe approach for Astro to take. |
|
Thanks for the feedback. For anyone hitting the ReferenceError: module is not defined crash during an Astro 5 → 6 migration with @astrojs/cloudflare, you can apply the alias manually at the project level as a workaround until this is resolved upstream. Here's how I did it in my own migration: nathanredblur/nathanredblur.dev@db7b501 The key is adding obug as a direct dependency and aliasing debug to a small shim in your astro.config.* — keeping it project-scoped rather than adapter-scoped, which avoids the safety concern raised here. astro.config.mjs src/shims/debug.js |
Changes
astro devwith@astrojs/cloudflarecurrently responds with HTTP 500 on every page request:The cause is the CJS
debugpackage. Its entrypoint (node_modules/debug/src/index.js) readsmodule.exportsat the top level, andmoduleis not a global in the workerd runner that@cloudflare/vite-pluginuses forastro dev, SSR and prerender environments, so evaluation throwsReferenceError: module is not defined.#15565 already addressed this for astro core by replacing its own
debugusage withobug(an ESM fork) and documented adding a Vite alias for the rehype/remark rabbit hole. In practice though,debugis pulled by a much broader set of transitive dependencies — e.g.micromark(via@astrojs/markdown-remark→remark-parse→mdast-util-from-markdown),stylus(via Vite's peer dep), and many others — so any Astro 6 project that uses markdown content or stylus transitively still crashes on every request.This PR extends the fix to user dependency graphs by aliasing
debugto an adapter-owned ESM shim in the Vite config the adapter installs. The shim forwards toobugand also re-exposes a default export, because consumers doimport debug from "debug"orrequire("debug")andobugonly ships named exports.What the shim looks like
Where the alias is applied
Top-level
vite.resolve.aliasinside the adapter'sastro:config:setuphook, so it propagates to every Vite environment created by the adapter (astro,ssr,prerender):obugis added as a direct dependency of@astrojs/cloudflareso the shim resolves inside the adapter's own package scope regardless of hoisting.Reproduction
Minimal reproduction — any Astro 6 project using
@astrojs/cloudflare@13with Markdown content orstylusin its dep tree will hit this. Starting fromnpm create astro@latestwith the Cloudflare adapter and one markdown page undersrc/content/is enough.Testing
@astrojs/cloudflare@13.3.0, Node 22.22.2). Before the patch:/,/blog/,/posts/:slug/,/about/,/archive/,/projects/all return HTTP 500 with themodule is not definedtrace above. With the built adapter dropped intonode_modulesand no other workaround: all the same routes return HTTP 200 with full content.pnpm --filter @astrojs/cloudflare build:ciproduces the expecteddist/shims/debug.jsalongside the patcheddist/index.js.debug; happy to add an integration test underpackages/integrations/cloudflare/test/fixtures/modelled afterastro-dev-platform/code-test.astroif maintainers want one — just point me at the preferred shape.Related
<Code ...>components #15284 — original "Astro v6 with cloudflare adapter does not work with<Code>components" issue (closed)debugwithobugDocs
N/A — internal adapter behavior change, transparent to users.