fix(miniflare): handle missing require.cache in getFreshSourceMapSupport (fix dev server crash under Yarn PnP)#13968
Merged
NuroDev merged 2 commits intoMay 28, 2026
Conversation
When `miniflare` is imported from ESM under Yarn PnP, Node's ESM->CJS bridge (`loadCJSModule` in `node:internal/modules/esm/translators`) hands the wrapped CJS module a re-invented `require` that only carries `.resolve` and `.main`, with no `.cache`. The cache-swap below needs a real CJS cache, so fall back to one built via `createRequire(__filename)` when we detect the stub. No behavior change on the normal CJS-loaded path. Fixes a `TypeError: Cannot read properties of undefined (reading '<path>')` crash in `wrangler dev` whenever the worker emits a structured log or the inspector forwards a stack trace, on Yarn PnP setups using Node 22-24.
Contributor
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
Contributor
|
Codeowners approval required for this PR:
Show detailed file reviewers |
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
🦋 Changeset detectedLatest commit: 5f81952 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
NuroDev
approved these changes
May 28, 2026
NuroDev
left a comment
Member
There was a problem hiding this comment.
Thanks for this change @danieltroger 🧡
workers-devprod
approved these changes
May 28, 2026
workers-devprod
left a comment
Contributor
There was a problem hiding this comment.
Codeowners reviews satisfied
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hits Yarn PnP setups using
wrangler devon Node 22 through 24. As soon as the worker emits a structured log or the inspector forwards a stack trace, wrangler crashes:require.cacheisundefinedat the failing line.Why: wrangler does
import "miniflare", Node's ESM loader asks Yarn PnP's loader hook for the source ofminiflare/dist/src/index.js, gets it back, and therefore routes throughloadCJSModuleinnode:internal/modules/esm/translatorsinstead ofloadCJSModuleWithModuleLoad. The Node translator path used when a hook returns asourcehands the wrapped CJS code a re-inventedrequirethat only carries.resolveand.main, no.cache. Confirmed by loggingObject.keys(require)->[ 'resolve', 'main' ]andString(require).slice(0, 200)-> the body ofloadCJSModule'srequireFn.Fix: when we detect the stub (no
.cache), reach for a real CJS require viacreateRequire(__filename)for the cache-swap dance. No behavior change on the normal CJS-loaded path.Related: yarnpkg/berry#7065 is the adjacent Node 25.7+
EBADFregression in the same area, but this symptom is independent and reproduces on Node 22 through 24.Repro
Node 24.15.0, Yarn 4.14.1 with
.yarnrc.ymlcontainingnodeLinker: pnp,wrangler@4.88.0/miniflare@4.20260504.0, plus anything that makes the worker emit a structured log:yarn patch, the crash is gone (0xTypeError) and worker stack traces source-map normally.