AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack: ESM Worker With Import.Meta.Url Path Fails With 'Cannot Find Module'

Using new Worker() with a path derived from import.meta.url in an ESM package under Next.js Turbopack throws 'Cannot find module', even though the worker file exists. The CommonJS version with __dirname works. This is a regression from Next.js 16.0.2.

highConfidence 80%Next.jsAffected V16.1.0Affected V16.2.0Affected V16.3.0Affected V16.3.7Affected V16.4.0-Canary.52

Origin Analysis

Turbopack's worker asset lookup table is keyed by file names (or simple relative paths), but the runtime argument constructed from import.meta.url is a full absolute file URL or absolute filesystem path, which does not match any key. This mismatch causes the worker module resolution to fail.
1. Clone https://github.com/orleib-lab/turbopack-worker-import-meta-repro\n2. Run npm install\n3. Run npm run build && npm start\n4. Open /api/esm and observe 'Cannot find module'\n5. Open /api/cjs and observe successful response

Fixing Code Block

Edge Case Audit

This workaround may not work if Turbopack re-emits the worker file under a different name or location; absolute paths may still fail if the worker registry uses original filenames. Revert to the original code once the upstream issue is resolved. Test in production build and dev before deploying.

Ecosystem Topology