Skip to content

v11.3.0 top-level await in ESM build breaks jsdom/vitest (ERR_REQUIRE_ASYNC_MODULE) #397

Closed

Description

@esetnik

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:

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.0
  • jsdom: 29.0.1
  • @asamuzakjp/css-color: 5.0.1–5.1.5 (all affected)
  • Node: 22+
  • Vitest: 4.1.2

Activity

  1. esetnik commented on Apr 6, 2026

    @esetnik
    Author

    Update on the workaround: The blanket "lru-cache": "11.2.7" override I suggested above can break projects that also depend on lru-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 constructor errors when Babel tries to use the v5 API against v11.

    Corrected workaround — scope the override to only affect v11+ consumers:

    "pnpm": {
      "overrides": {
        "lru-cache@>=11": "11.2.7"
      }
    }

    This preserves lru-cache@5.1.1 for 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-color to a version that doesn't pull in lru-cache 11.3.0, if one exists).

  2. thinkasany commented on Apr 6, 2026

    @thinkasany

    The same problem.

  3. AviVahl commented on Apr 6, 2026

    @AviVahl

    also appears to break our flow that uses mocha

    EDIT:
    can be seen with

    mkdir lru-cache-issue
    cd lru-cache-issue
    npm init -y
    npm pkg set type=module
    npm i jsdom
    node -e "console.log(require('jsdom'))"
    node -e "import('jsdom').then(console.log)"
  4. manojsridharsrinivasan commented on Apr 6, 2026

    @manojsridharsrinivasan

    @esetnik I see the issue is closed, has a new version been released for this?

  5. reopened this on Apr 6, 2026
  6. isaacs commented on Apr 6, 2026

    @isaacs
    Owner

    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?

  7. isaacs commented on Apr 6, 2026

    @isaacs
    Owner

    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 import condition rather than the module-sync condition.

  8. isaacs commented on Apr 6, 2026

    @isaacs
    Owner

    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?

  9. esetnik commented on Apr 7, 2026

    @esetnik
    Author

    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?

    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.

  10. binarykitchen commented on Apr 7, 2026

    @binarykitchen

    Likewise, big thanks for your hard work @isaacs

    Image

  11. isaacs commented on Apr 7, 2026

    @isaacs
    Owner

    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 node as an override dialect 😅 C'est la vie, maybe some day commonjs will finally die and we can stop worrying about this lol.

  12. jsg2021 commented on Apr 8, 2026

    @jsg2021

    :( 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.

  13. isaacs commented on Apr 8, 2026

    @isaacs
    Owner

    @jsg2021 If you use the esm path, but not the node path, 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 the node path, 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.

  14. added a commit that references this issue on Apr 8, 2026
    28e4dac
  15. jsg2021 commented on Apr 8, 2026

    @jsg2021

    Pinning to an old version indefinitely is a bad strategy.

    I agree. It's just temporary.

    You probably can reproduce with npx create-expo-app@latest and then import this lib in app/_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.

  16. isaacs commented on Apr 8, 2026

    @isaacs
    Owner

    @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 node import path, but doesn't have the node builtin 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.

  17. jsg2021 commented on Apr 8, 2026

    @jsg2021

    @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

  18. isaacs commented on Apr 8, 2026

    @isaacs
    Owner

    If metro falls back to the node import condition, but doesn't provide a node environment, then that's a bug in metro.

    When I put a build behind the node import 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 the node condition 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions