Skip to content

require(esm) error caught by try/catch also generates unhandled rejection #56267

Closed
@hi-ogawa

Description

@hi-ogawa

Version

v22.12.0

Platform

Linux myhostname 6.12.1-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 22 Nov 2024 16:04:27 +0000 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

reproduction: https://github.com/hi-ogawa/reproductions/tree/main/node-require-esm-error-try-catch

Given two files:

  • require-esm.cjs
function main() {
  process.on('uncaughtException', (e) => {
    console.log("[uncaughtException]", e)
  });

  process.on('unhandledRejection', (e) => {
    console.log("[unhandledRejection]", e)
  });

  try {
    require("./error.mjs")
  } catch (e) {
    console.log("[try/catch require(esm)]", e)
  }
}

main();
  • error.mjs
throw new Error("boom")

running node reqire-esm.cjs shows a following

[try/catch require(esm)] Error: boom  <===== 👈 log from try/catch
    at file:///home/hiroshi/code/personal/reproductions/node-require-esm-error-try-catch/src/error.mjs:1:7
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:395:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at Object.loadESMFromCJS [as .mjs] (node:internal/modules/cjs/loader:1414:24)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)
(node:76690) ExperimentalWarning: CommonJS module /home/hiroshi/code/personal/reproductions/node-require-esm-error-try-catch/src/require-esm.cjs is loading ES Module /home/hiroshi/code/personal/reproductions/node-require-esm-error-try-catch/src/error.mjs using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
[unhandledRejection] Error: boom  <===== 👈 log from unhandled rejection
    at file:///home/hiroshi/code/personal/reproductions/node-require-esm-error-try-catch/src/error.mjs:1:7
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:395:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at Object.loadESMFromCJS [as .mjs] (node:internal/modules/cjs/loader:1414:24)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

I'm not entirely sure the semantics of require(esm) and top level errors, but I'd expect there's no unhandled rejection.

What do you see instead?

unhandled rejection

Additional information

I was checking odd issues with tailwind config loading in vitejs/vite#18969 and ended up with the minimal repro here to confirm whether the current behavior is intended. I think it's somewhat common to use try/catch with require with some fallback and tailwind seems to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions