[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"
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
- Domain
- build-system, testing
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-plugin1.9.2.vitest4.1.9,vite8.3.8,wrangler4.- 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'sbrowsermain field and conditions andssr.target: "webworker". Vite then applies postcss's"source-map-js": false, and the require is redirected to the missing stub. - Plugin inside a
test.projectsentry: the root server keeps Vite's defaults, so thebrowserfield is ignored andsource-map-jsresolves to the real package. The same repro passes (3/3) when the config is moved intotest.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
.ctstoo, for example by widening the filter to/\.[cm]?ts$/. I haven't checked what tsdown emits for a.ctsentry underformat: "esm". - Alternatively, ship the stub as a static file.
- A test that every path the pool redirects to (
emptyLibPathand the otherlibPathtargets) 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
- 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