ERR_REQUIRE_ASYNC_MODULE node module_error ai_generated true

Error [ERR_REQUIRE_ASYNC_MODULE]: require() of ES Module not supported with top-level await

ID: node/err-require-async-module

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Trying to require() a module that uses top-level await. Must use import() instead.

generic

Workarounds

  1. 95% success Use dynamic import: const mod = await import('module')
    const mod = await import('module')

    Sources: https://nodejs.org/api/esm.html#import-expressions

  2. 88% success Convert calling code to ESM with 'type': 'module' in package.json
    'module' in package.json

    Sources: https://nodejs.org/api/packages.html#type

Dead Ends

Common approaches that don't work:

  1. Remove top-level await from the module 65% fail

    May break the module's initialization logic

Error Chain

Leads to: