cloudflare / cloudflare/workerd

Bundled worker modules have undefined import.meta.url, crashing fileURLToPath(import.meta.url) callers

Open
#6,986 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

### Describe the bug

Bundled modules running inside workerd see `import.meta.url` as `undefined`, so any library that calls `fileURLToPath(import.meta.url)` or `createRequire(import.meta.url)` at module init crashes with `TypeError: The "path" argument must be of type string or an instance of URL. Received undefined`.

### Repro

Minimal worker built with Vite 8 (rolldown) and deployed via `wrangler deploy`:

```ts
// src/index.ts
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url); // TypeError in workerd

export default {
fetch() {
return new Response(__filename);
},
} satisfies ExportedHandler;
```

Bundled module init: `import.meta.url` is `undefined` → `fileURLToPath` throws → the Worker fails to start.

### Impact

Real-world: Payload CMS (3.82) cannot initialize on Workers — its `payload.config.ts` and transitive deps (findUp-style package.json discovery in `getPackageJSON`) call `fileURLToPath(import.meta.url)` at module init. We hit this running Payload on vinext/Workers; a third-party compatibility plugin has been AST-patching this pattern across multiple vinext releases, confirming it reproduces on real Workers, not just local Miniflare.

### Expected behavior

`import.meta.url` should be defined for bundled modules (e.g. a `file://` URL or the bundle's virtual URL), or at minimum the crash should be a documented, catchable behavior rather than module-init failure.

### Environment

- wrangler 4.116.0 / workerd 1.x (current)
- Vite 8.2.1 / rolldown
- Node 24
- Windows 11 (also reproduced conceptually platform-independent)

### Related

- Third-party patch that works around this: `workerd-import-meta-url-guard` in [vite-plugin-vinext-payload](https://github.com/block65/vite-plugin-vinext-payload) — guards `fileURLToPath(import.meta.url)` with `?? "file:///"`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.