Repository navigation
Description
Summary
lru-cache@11.3.0 (PR #395) introduced a top-level await in the ESM build via src/diagnostics-channel-esm.mts:
export const [metrics, tracing] = await import('node:diagnostics_channel').then(...).catch(...)This breaks any CJS module that transitively loads the ESM build of lru-cache, most notably jsdom via @asamuzakjp/css-color (which is pure ESM with no CJS entrypoint).
Dependency chain
jsdom (CJS)
→ @asamuzakjp/css-color (pure ESM, "type": "module")
→ lru-cache@11.3.0 (ESM build now has TLA)
→ ERR_REQUIRE_ASYNC_MODULE
Error
Error: require() cannot be used on an ESM graph with top-level await.
Use import() instead. To see where the top-level await comes from,
use --experimental-print-required-tla.
From jsdom/lib/jsdom/living/css/helpers/css-values.js
Requiring @asamuzakjp/css-color/dist/esm/index.js
This causes every Vitest test using jsdom to fail — no tests run at all.
Impact
This is affecting projects using Vitest + jsdom on Node 22+. The issue surfaced broadly today via Renovate/Dependabot lock file maintenance PRs that re-resolved lru-cache@^11.x to 11.3.0.
Related jsdom issues filed today:
- jsdom broken on Node 24 due to downstream top-level await jsdom/jsdom#4138 — "jsdom broken on Node 24 due to downstream top-level await"
- [Discussion]: require() cannot be used on an ESM graph with top-level await. Use import() instead. jsdom/jsdom#4137 — "require() cannot be used on an ESM graph with top-level await"
Also related: #396 (Angular test runner memory leak from the same diagnostics-channel feature).
Why this happens
The exports map correctly routes require → CJS and import → ESM. However, when a CJS package (jsdom) requires a pure-ESM package (@asamuzakjp/css-color), Node resolves the ESM graph. Within that ESM graph, lru-cache's ESM entrypoint is loaded — which now contains TLA, making the entire graph impossible to require() synchronously.
Suggested fix
The comment in the source notes: "the first tick of metrics will be missed but that probably doesn't matter much." This suggests a non-blocking approach would be acceptable:
// Instead of TLA:
// export const [metrics, tracing] = await import('node:diagnostics_channel')...
// Use non-blocking init:
let metrics = dummyMetrics, tracing = dummyTracing;
import('node:diagnostics_channel').then(dc => {
metrics = dc.channel('lru-cache:metrics');
tracing = dc.tracingChannel('lru-cache:tracing');
}).catch(() => {});
export { metrics, tracing };This preserves the non-Node fallback behavior without introducing TLA.
Workaround
Pin lru-cache to 11.2.7:
"pnpm": {
"overrides": {
"lru-cache": "11.2.7"
}
}Versions
lru-cache: 11.3.0jsdom: 29.0.1@asamuzakjp/css-color: 5.0.1–5.1.5 (all affected)- Node: 22+
- Vitest: 4.1.2
Activity
esetnik commentedon Apr 6, 2026
Update on the workaround: The blanket
"lru-cache": "11.2.7"override I suggested above can break projects that also depend onlru-cache@5.x(e.g. via@babel/helper-compilation-targets), since pnpm overrides force all resolutions of that package to the specified version — causing_lruCache is not a constructorerrors when Babel tries to use the v5 API against v11.Corrected workaround — scope the override to only affect v11+ consumers:
This preserves
lru-cache@5.1.1for Babel while pinning v11.x consumers to 11.2.7 (pre-TLA).For npm/yarn users who can't scope overrides by version range, you may need to pin the specific intermediate packages instead (e.g. override
@asamuzakjp/css-colorto a version that doesn't pull in lru-cache 11.3.0, if one exists).fix: patch `lru-cache` ESM build to remove top-level await
thinkasany commentedon Apr 6, 2026
The same problem.
AviVahl commentedon Apr 6, 2026
also appears to break our flow that uses mocha
EDIT:
can be seen with
manojsridharsrinivasan commentedon Apr 6, 2026
@esetnik I see the issue is closed, has a new version been released for this?
temp: Pin lru cache to get vitest test running again
fix(frontend): pin lru-cache@11.2.7 to avoid TLA breakage
isaacs commentedon Apr 6, 2026
So, ok, there's pure-ESM packages being loaded with CommonJS
require(), and thus the entire ecosystem has to never use TLA?That seems like a bizarre take. This feels more like a bug in the pure-ESM package, not this one, which accurately and responsibly guards the use of TLA behind an ESM-only export, no?
isaacs commentedon Apr 6, 2026
Or rather, a bug in the CommonJS package that's loading ESM without guaranteeing that nothing in the transitive dependnecy chain uses anything that's ESM-only.
There's a reason I'm exporting it with the
importcondition rather than themodule-synccondition.isaacs commentedon Apr 6, 2026
I guess I could do a node+esm build that doesn't use TLA? But this is a sucky situation, how is everyone ok with it being like this?
esetnik commentedon Apr 7, 2026
Yeah, I agree. I'm not sure what the best approach is here. I just documented it because I figured a lot of other people were about to hit the same failure and have to dig deep into their transitive dependencies to uncover what was going on. @isaacs thanks for your work.
binarykitchen commentedon Apr 7, 2026
Likewise, big thanks for your hard work @isaacs
isaacs commentedon Apr 7, 2026
Well, regardless, this is fixed on v11.3.2, and makes me feel even more glad that I just added the feature in tshy to be able to use
nodeas an override dialect 😅 C'est la vie, maybe some day commonjs will finally die and we can stop worrying about this lol.jsg2021 commentedon Apr 8, 2026
:( I don't want to rain on any parades, but react-native environments don't seem to have node:diagnostics_channel and use the esm path.
I'm pinning to 11.2.7 for now.
temp: Pin lru cache to get vitest test running again
isaacs commentedon Apr 8, 2026
@jsg2021 If you use the
esmpath, but not thenodepath, then it should work, because it does a dynamic import, and falls back to a dummy shim if not present. And if it is using thenodepath, but not actually node and not providing the built-in modules that node provides, then that's a bug in the non-node platform, I'd think.Can you share anything about your environment so that I can debug the situation? Pinning to an old version indefinitely is a bad strategy.
Remove TLA, feature is DOA :(
jsg2021 commentedon Apr 8, 2026
I agree. It's just temporary.
You probably can reproduce with
npx create-expo-app@latestand then import this lib inapp/_layout.tsx.The metro bundler is throwing a resolve error. So, I'm thinking I may have to alias the node module in Metro's config. I'm wondering if any bundler would need aliasing.
isaacs commentedon Apr 8, 2026
@jsg2021 Yeah, that worked for me in a fresh
create-expo-app. It loads the node version of the code, and diagnostics_channel support is there, afaict. But it also seems to be actually running in node, maybe doing some kind of SSR? I'm really not familiar with react-native, sorry.Can you try with version 11.3.3? It should not rely on TLA any longer. If it's still pulling from the
nodeimport path, but doesn't have thenodebuiltin modules, then idk what to tell you. If platforms are going to lie about the features they support, and we act on that misleading information, then there's really nothing that library authors can hope to do. That's something to report to your build system, perhaps apply some kind of polyfill or workaround.jsg2021 commentedon Apr 8, 2026
@isaacs I'll try that. However, I don't think the TLA was the problem. I think bundlers will see the package and attempt to bundle it, even if the import is dynamic. So my theory is that we add a platform target in the exports map that diverts the bundler from seeing the missing package. Webpack looks for "browser" by default, but can be configured. React-Native (Metro bundler) looks for "react-native"... and falls back to "node".
https://metrobundler.dev/docs/package-exports/#replacing-browser-and-react-native-fields
isaacs commentedon Apr 8, 2026
If metro falls back to the
nodeimport condition, but doesn't provide a node environment, then that's a bug in metro.When I put a build behind the
nodeimport condition, that means it's for a node environment; if a platform loads it, and doesn't provide a node env, it's going to break, and there's nothing I can reasonably do to avoid it. They need to not import thenodecondition if they are not providing the builtin modules found in a node environment.If they break at build time when encountering an unknown module being loaded via dynamic
import(), rather than just treating it as a rejected Promise at runtime, then that also means that they have removed the only reasonable mechanism for feature detection. That's bug in the bundler.Please report the bug to them, if that is the case. I am not able to fix these issues for you.