Repository navigation
Description
Astro Info
Astro v7.4.0
Vite v8.3.4
Node v24.13.1
System macOS (arm64)
Package Manager pnpm
Output server
Adapter @astrojs/cloudflare (v14.4.0)
Integrations none
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
astro dev with @astrojs/cloudflare 14 fails on the first request when a server-side dependency is CommonJS that requires node-fetch. cross-fetch is the common case: its Node entry does require('node-fetch') at module load.
Internal server error: Calling `require` for "node-fetch" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.
at runInRunnerObject (workers/runner-worker/index.js:107:3)
at Object.getComponentByRoute (.../node_modules/.vite/deps_ssr/astro_app_entrypoint_dev.js:52:9)
...
Cause. vite-plugin-environment hard-codes optimizeDeps.exclude: ["node-fetch"] for the server environments (ssr, astro, prerender, client) whenever dependency discovery is on (dist/vite-plugin-environment/index.js, the noDiscovery === false branch). So Vite pre-bundles cross-fetch for the workerd ssr environment but leaves node-fetch external. Under Vite 8 (Rolldown), an external that a bundled CommonJS dependency requires stays a bare require("node-fetch") call, and workerd has no require.
astro build is fine; only dev is affected.
Workaround (confirmed in the repro below): a post plugin that removes that one exclude for the ssr environment. After that, node-fetch is bundled into the optimized dep and the page renders.
const keepNodeFetch = {
name: 'keep-node-fetch',
enforce: 'post',
configEnvironment(name, options) {
if (name !== 'ssr' || !options.optimizeDeps?.exclude) return;
options.optimizeDeps.exclude = options.optimizeDeps.exclude.filter((id) => id !== 'node-fetch');
},
};
// defineConfig({ ..., vite: { plugins: [keepNodeFetch] } })Re-adding node-fetch to optimizeDeps.include, changing resolve.mainFields, and Rolldown's esmExternalRequirePlugin did not fix it.
It's unclear whether the exclude is still needed for the workerd environment. If it exists for the Node runtime, it probably shouldn't apply when the adapter runs ssr in workerd.
What's the expected result?
astro dev serves the page, as astro build does and as it did on Astro 6 / Vite 7.
Link to Minimal Reproducible Example
StackBlitz doesn't support @astrojs/cloudflare, so here are the four files inline. Run pnpm install, then pnpm dev, then request /. It returns 500 with the error above. Add the workaround plugin to the config and it returns 200.
package.json
{
"name": "astro-node-fetch-repro",
"type": "module",
"private": true,
"scripts": { "dev": "astro dev", "build": "astro build" },
"dependencies": {
"astro": "7.4.0",
"@astrojs/cloudflare": "14.4.0",
"cross-fetch": "4.1.0",
"wrangler": "*"
}
}astro.config.mjs
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';
export default defineConfig({
output: 'server',
adapter: cloudflare(),
});src/pages/index.astro
---
// cross-fetch's CommonJS entry does `require('node-fetch')` at module load.
import crossFetch from 'cross-fetch';
---
<p>{typeof crossFetch}</p>wrangler.jsonc
{ "name": "astro-node-fetch-repro", "compatibility_date": "2026-10-01", "compatibility_flags": ["nodejs_compat"] }Participation
- I am willing to submit a pull request for this issue.
Activity
astro-factory commentedon Oct 18, 2026
Triage complete
triage: fix pendingpackages/astro/test/units/vite-plugin-environment/server-entries.test.tsastro devwith the Cloudflare adapter when a CJS dependency requiresnode-fetch. The reporter's small Vite plugin works around it completely.In
packages/astro/src/vite-plugin-environment/index.ts(around line 91), server environments that turn on dependency discovery (noDiscovery === false) get a hard-codedoptimizeDeps.exclude: ['node-fetch']. In practice that means the Cloudflare workerd environments. So when Vite bundlescross-fetchfor the workerdssrenvironment,node-fetchstays external, and Rolldown emits__require("node-fetch")in the optimized dep. That call throws because workerd has norequire. Git history suggests the exclude is a leftover: it used to be in the top-level (client)optimizeDepsconfig increate-vite.ts, and the Environment API refactor (#14306) moved it as-is into the server environment branch. No comment or test explains why it's needed there.I found a potential fix for this issue. The branch removes
'node-fetch'from that exclude, so the optimizer bundlesnode-fetchinto deps that require it. With the reporter's config unchanged,GET /now returns 200. The existing Cloudflare dev tests pass. This needs human review.Try this fix
You can test this fix right now without waiting for a release:
# astro npm i https://pkg.pr.new/astro@4fac8a0If this fixes your issue, please leave a comment letting us know (for example
"confirmed, this fixes it"). A pull request will then be opened to get it merged.
Full Triage Report
Issue summary
astro devwith@astrojs/cloudflare14 fails on the first request when a server-side CJS dependency callsrequire('node-fetch'), for examplecross-fetch:astro buildworks. Expected:astro devserves the page, likeastro builddoes and like Astro 6 / Vite 7 did.Environment
@astrojs/cloudflare14.4.0, outputservermain(astro 7.4.0 and @astrojs/cloudflare 14.4.0 linked from the workspace), Vite 8.3.4, wrangler 4.149.0, workerd 1.20261006.1, cross-fetch 4.1.0, Node 22.23.2, pnpm 10.34.6, LinuxSteps to reproduce
astro.config.mjs:output: 'server', adapter: cloudflare()src/pages/index.astro:import crossFetch from 'cross-fetch';and render<p>{typeof crossFetch}</p>wrangler.jsonc:{ "name": "astro-node-fetch-repro", "compatibility_date": "2026-10-01", "compatibility_flags": ["nodejs_compat"] }pnpm install,astro dev, then request/Result
node_modules/.vite/deps_ssr/cross-fetch.jscontainsvar nodeFetch = __require("node-fetch");node-fetchfromssr'soptimizeDeps.exclude),/returns 200.astro buildsucceeds.Diagnosis
packages/astro/src/vite-plugin-environment/index.ts, lines 82-93:node-fetchkeeps it external. Rolldown then leaves a__require("node-fetch")call inside the pre-bundled CJS dependency, and that call throws in workerd.optimizeDepsincreate-vite.ts(exclude: ['astro', 'node-fetch']), which only affected the client optimizer. Environment API #14306 copied it into the server environment branch. fix: add Cloudflare e2e tests w/ pre-optimized deps #14975 (f73fab4) later removed'astro'but kept'node-fetch'. No comment or test explains whynode-fetchis excluded for server environments.Verification
node-fetchcan't load in workerd with the exclude in place anyway.Fix
packages/astro/test/units/vite-plugin-environment/server-entries.test.ts, "does not exclude node-fetch from server dependency optimization". It checks thatconfigEnvironment('ssr', { optimizeDeps: { noDiscovery: false } })doesn't putnode-fetchinexclude. All 12 tests in that folder pass.astro devwith the original config (no workaround) returns 200 and renders<p>function</p>.packages/integrations/cloudflare/test/astro-dev-platform.test.tspasses 8/8. Biome is clean on the changed files..changeset/fix-cloudflare-node-fetch.md(astro: patch).This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.
rotexhawk commentedon Oct 18, 2026
Confirmed, this fixes it. Tested with
astro@https://pkg.pr.new/astro@4fac8a0, without the workaround plugin:astro devnow serves/with 200 and renders<p>function</p>, with norequireerror.astro buildstill passes.@astrojs/cloudflare14.4.0 withcopc→cross-fetch→node-fetch, workaround removed.astro devboots, the SSR pages return 200, and our end-to-end funnel test passes.Thanks for the quick turnaround.
astro-factory commentedon Oct 18, 2026
The fix has been verified! A pull request has been created: #18438