Vite dev crashes on CJS requires of excluded non-builtin packages

Open
#16,769 2 comments 0 reactions 0 assignees View on GitHub

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

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

bug package:vite-plugin
What versions & operating system are you using?
  • @cloudflare/vite-plugin: 2.0.0-beta.v.12, with cf 1.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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.