Skip to content

[feat]: Don't panic ERR_REQUIRE_ASYNC_MODULE when using try/catch approach, it should thrown only when top await executedΒ #58032

@JounQin

Description

@JounQin

What is the problem this feature will solve?

/** @type {Plugin[]} */
let plugins

try {
  plugins = dependencies.map(pkgName => {
    /** @type {Plugin} */
    const pkg = require(pkgName)
    return pkg.default || pkg
  })
} catch {
  plugins = await Promise.all(
    dependencies.map(async pkgName => {
      /** @type {Plugin} */
      const pkg = await import(pkgName)
      return pkg.default || pkg
    }),
  )
}

What is the feature you are proposing to solve the problem?

I'd like to use require(ESM) when possible but fallback to top await when it fails.

What alternatives have you considered?

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions