Skip to content

Conversation

@CloudyWSA
Copy link
Contributor

@CloudyWSA CloudyWSA commented Jan 28, 2026

Changes

  • Dynamically detect and externalize all Node.js built-ins (fs, os, path, etc.) only during dev mode.

  • Added these modules to optimizeDeps.exclude and ssr.external within the Cloudflare adapter configuration.

  • Updated the vite:cf-imports plugin to resolve node: 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 a workerd target, 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 dev after linking the modified adapter.

Docs

  • No documentation changes needed.
@changeset-bot
Copy link

changeset-bot bot commented Jan 28, 2026

🦋 Changeset detected

Latest 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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Jan 28, 2026
@n3dst4
Copy link

n3dst4 commented Jan 28, 2026

Does this fix the initial error:

The file does not exist at "[PATH TO PROJECT]/node_modules/.vite/deps_ssr/chunk-BB2NZPL6.js?v=c17d9a8e" which is in the optimize deps directory.

Or just the error on subsequent runs:

Failed to load url node:fs/promises (resolved id: node:fs/promises) in 

(I don't know Astros internals and from looking at the changes I thought it might only be fixing the second one.)

@CloudyWSA
Copy link
Contributor Author

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.

@n3dst4
Copy link

n3dst4 commented Jan 28, 2026

Thank you, great explanation.

Copy link
Member

@ematipico ematipico left a 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.

Copy link
Contributor

@matthewp matthewp left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

4 participants