[vitest-plugin] `empty-internal.cjs` is missing from the published package, so requiring a module that a `browser` field maps to `false` fails with "No such module"

Open Beginner friendly
#17,165 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript, vite

Research direction

Start with packages/vitest-plugin/tsdown.config.ts and the empty-internal.cts entry under packages/vitest-plugin/src/worker/lib/cloudflare/; the issue identifies the build filter that omits it. Check the packed package to confirm the worker stub is included, then reproduce with the inline postcss test using npm install && npx vitest run. Done when the fallback no longer fails because empty-internal.cjs is missing.

Written by the indexing model from the issue text.

Description

What versions & operating system are you using?
  • @cloudflare/vitest-plugin 1.9.2.
  • vitest 4.1.9, vite 8.3.8, wrangler 4.
  • Node 24, Linux x64.
Please provide a link to a minimal reproduction

The repro is inline below. It fails the same way with npm's flat layout, so it isn't specific to pnpm.

package.json

{
  "private": true,
  "type": "module",
  "devDependencies": { "@cloudflare/vitest-plugin": "1.9.2", "vitest": "4.1.9", "wrangler": "^4" },
  "dependencies": { "postcss": "8.5.33" }
}

wrangler.jsonc

{ "name": "repro", "main": "src/index.js", "compatibility_date": "2026-04-16", "compatibility_flags": ["nodejs_compat"] }

src/index.js

export default { fetch: () => new Response("ok") };

vitest.config.js

import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({ plugins: [cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" } })] });

test/repro.test.js

import { expect, it } from "vitest";
it("loads postcss", async () => {
  const { default: postcss } = await import("postcss");
  expect(typeof postcss).toBe("function");
});

Run npm install && npx vitest run.

Describe the Bug

postcss declares "browser": { "source-map-js": false, … }. Its lib/input.js does require('source-map-js'). Vite resolves that to __vite-browser-external, and viteResolve() in src/pool/module-fallback.ts maps that id to emptyLibPath, i.e. dist/worker/lib/cloudflare/empty-internal.cjs. That file isn't in the package, so the fallback service fails with ENOENT and workerd reports No such module …/postcss/lib/source-map-js:

require(".../node_modules/postcss/lib/source-map-js") relative to .../postcss/lib/input.js?mf_vitest_no_cjs_esm_shim:
  redirect: .../node_modules/@cloudflare/vitest-plugin/dist/worker/lib/cloudflare/empty-internal.cjs?mf_vitest_no_cjs_esm_shim
require(".../@cloudflare/vitest-plugin/dist/worker/lib/cloudflare/empty-internal.cjs?mf_vitest_no_cjs_esm_shim") ...:
  error: Error: ENOENT: no such file or directory

The source file exists at packages/vitest-plugin/src/worker/lib/cloudflare/empty-internal.cts. The worker build in tsdown.config.ts only takes lib entries matching /\.m?ts$/, so the .cts file is never built:

...libPaths.filter((libPath) => /\.m?ts$/.test(libPath)),

dist/worker/lib/cloudflare/ ships only snapshot.mjs, test-internal.mjs and test.mjs.

As a result, every require() that Vite resolves to __vite-browser-external fails in the pool. postcss is one example, and through it every package that loads postcss, such as sanitize-html.

When it triggers

It depends on where cloudflareTest() is registered. The module fallback service resolves with the root Vite server, not the project's (handleModuleFallbackRequest.bind(void 0, ctx.vite), where ctx is the Vitest instance):

  • Plugin in the root plugins (the repro above, and the layout the docs show): the root server gets the plugin's browser main field and conditions and ssr.target: "webworker". Vite then applies postcss's "source-map-js": false, and the require is redirected to the missing stub.
  • Plugin inside a test.projects entry: the root server keeps Vite's defaults, so the browser field is ignored and source-map-js resolves to the real package. The same repro passes (3/3) when the config is moved into test.projects.

This also means that in a projects setup, require()s inside node_modules are resolved without the project's workerd/worker/browser conditions. That may be worth looking at separately.

Possible fix
  • Build the .cts too, for example by widening the filter to /\.[cm]?ts$/. I haven't checked what tsdown emits for a .cts entry under format: "esm".
  • Alternatively, ship the stub as a static file.
  • A test that every path the pool redirects to (emptyLibPath and the other libPath targets) exists in the packed tarball would stop this from regressing.
Please provide any relevant error logs
[vitest-plugin] Failed to require ".../@cloudflare/vitest-plugin/dist/worker/lib/cloudflare/empty-internal.cjs?mf_vitest_no_cjs_esm_shim" from ".../node_modules/postcss/lib/input.js?mf_vitest_no_cjs_esm_shim".
Error: No such module ".../node_modules/postcss/lib/source-map-js".
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.