Vite dev crashes on CJS requires of excluded non-builtin packages
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 53/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- javascript, node.js, typescript, vite
- Domain
- build-system, devtools
Research direction
Start with the linked minimal reproduction and inspect the Worker plugin’s dependency optimization configuration, especially its use of esmExternalRequirePlugin and Vite’s optimizer behavior on the neutral platform. Reproduce the failure and compare it with the supplied workaround for the excluded esm-peer package. Done means the fixture starts successfully and returns HTTP 200 with SSR OK, with the handling or configuration documented for this class of exclusions.
Written by the indexing model from the issue text.
Description
What versions & operating system are you using?
@cloudflare/vite-plugin: 2.0.0-beta.v.12, withcf1.0.0-beta.28- Vite: 8.3.6 (plain Vite, not Vite+)
- Node: 26.13.0
- pnpm: 12.18.0
- macOS: 27.2, arm64
Also reproduced with @cloudflare/[email protected], [email protected], and Vite 8.3.6.
Please provide a link to a minimal reproduction
https://gist.github.com/hckhanh/f85e5e537d7ce73e954f2d74390b46b9
The gist includes a lockfile and two tiny local fixture packages. It needs no React, Redact, TanStack Start, RSC, Vite+, credentials, bindings, or deployment.
Describe the Bug
A dependency prebundled in the Worker SSR environment cannot require() another package listed in optimizeDeps.exclude, even when that excluded package is an ESM module the runner could import.
The fixture is:
// cjs-consumer/index.cjs
module.exports = require('esm-peer').value
// esm-peer/index.js (type: module)
export const value = 'SSR OK'
// worker.js
import value from 'cjs-consumer'
export default { fetch() { return new Response(value) } }
Relevant Vite configuration:
plugins: [cloudflare({ viteEnvironment: { name: 'ssr' } })],
environments: {
ssr: {
optimizeDeps: {
include: ['cjs-consumer'],
exclude: ['esm-peer'],
},
},
},
Steps to reproduce
Save/clone the gist into an empty directory:
node setup.mjs
pnpm install --ignore-scripts --frozen-lockfile
node check.mjs
Expected: the worker returns HTTP 200 and SSR OK.
Actual: server initialization fails while the runner inspects the worker exports, because the optimized CJS consumer still calls require("esm-peer").
Verified workaround
Add this to environments.ssr.optimizeDeps.rolldownOptions.plugins:
esmExternalRequirePlugin({ external: ['esm-peer'] })
// imported from 'vite'
The gist enables it with:
REPRO_FIX=1 node check.mjs
Both tested plugin versions then return:
{"fix":"1","status":200,"body":"SSR OK"}
The excluded module is not also listed in top-level Rolldown external.
Investigation / relationship to #11948
This appears to be a remaining case beyond #11948's fix for Node built-ins. The installed Cloudflare plugin configures dependency optimization with platform: 'neutral' and adds esmExternalRequirePlugin({ external: [nodeBuiltinsRE], ... }); that conversion does not match a user-excluded package such as esm-peer.
Vite deliberately skips its CJS external facade on the neutral platform, so I am reporting this here rather than as a Vite regression. Rolldown's explicit ESM conversion plugin handles the minimal case.
The real-app trigger was @tanstack/redact/[email protected] excluding React peers to preserve one aliased runtime, while @vitejs/[email protected] prebundled its CommonJS react-server-dom/client.edge vendor in the SSR environment. Dev SSR failed on require("react-dom"). The minimal repro above removes all those framework dependencies.
Could the Worker optimizer handle this class of configured exclusions, or document where the necessary conversion should be supplied? I realize converting every external require can change interop/resolution semantics; I am not assuming an unconditional conversion is safe for all packages.
Please provide any relevant error logs
Error: Calling `require` for "esm-peer" in an environment that doesn't expose the `require` function.
at runInRunnerObject (./workers/runner-worker/index.js:107:3)
at getWorkerEntryExportTypes (./workers/runner-worker/index.js:252:24)
at null.<anonymous> (./workers/runner-worker/index.js:356:37)
at maybeCaptureError (./workers/runner-worker/index.js:51:10)
- Dominant language
- TypeScript
- Stars
- 4.7k
- Forks
- 1.7k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 282
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudflare/workers-sdk
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
cloudflare/workers-sdk#17215 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
cloudflare/workers-sdk#17189 · 3 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workers-sdk#17187 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
cloudflare/workers-sdk#17175 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
cloudflare/workers-sdk#17174 ·
Maintainers usually reply within 1 day
All issues in cloudflare/workers-sdk
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
earendil-works/pi#12615 ·
Maintainers usually reply within 1 day
-
My Caps shows "Shared" on private Caps that were never sharedPossibly taken @AksharP5 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
CapSoftware/Cap#4569 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
OHDSI/Data2Evidence#3749 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Flagsmith/flagsmith-nodejs-client#332 ·
Maintainers usually reply within 2 days