-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(cloudflare): externalize node built-ins in dev mode #15326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 56e86fd 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 |
|
Does this fix the initial error: Or just the error on subsequent runs: (I don't know Astros internals and from looking at the changes I thought it might only be fixing the second one.) |
|
The “Missing Chunk” error (Error 1) happens because Vite tries to pre-bundle dependencies that end up pulling in Node built-ins. Since the Cloudflare adapter targets webworker, this conflicts with the optimizer and Vite can’t generate or resolve the expected chunks in .vite/deps_ssr. Excluding nodeBuiltins from optimizeDeps prevents Vite from touching those modules, which avoids the issue entirely. The node:fs/promises error (Error 2) happens during SSR when Vite tries to resolve Node-only imports for a webworker target. Because that runtime doesn’t support Node built-ins, resolution fails. Marking them as external tells Vite to leave those imports alone. This works during astro dev since SSR still runs on Node. |
|
Thank you, great explanation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @CloudyWSA for your PR. I noticed that you filed the issue for Astro v6, but you sent the fix to main. The main branch contains the code of Astro v5.
You have to pull the next branch, and send the fix to the next branch. The code of the cloudflare adapter changed a lot, so make sure to get used to the new logic.
Also, rely on the use of the environment APIs to tweak the configuration of vite.
matthewp
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, @ematipico is right here, should go against next.
Changes
Dynamically detect and externalize all Node.js built-ins (
fs,os,path, etc.) only duringdevmode.Added these modules to
optimizeDeps.excludeandssr.externalwithin the Cloudflare adapter configuration.Updated the
vite:cf-importsplugin to resolvenode:prefixed imports as external.Why? Astro's image optimization endpoint (
astro/assets/endpoint/dev.js) relies on Node.js APIs to serve local images during development. Since the adapter sets aworkerdtarget, Vite was attempting to bundle these modules and failing. This change ensures the Vite SSR runner treats them as external in the dev environment while keeping the production bundle strict.Fixes #15319
Testing
Tested locally with a reproduction project using the
<Image />component.Verified that the "Failed to load url node:fs/promises" error is gone and images render correctly in
pnpm devafter linking the modified adapter.Docs