Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hi-ogawa opened this issue Dec 16, 2024 · 4 comments
Closed
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@hi-ogawa
Copy link

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.

@targos
Copy link
Member

targos commented Dec 16, 2024

/cc @nodejs/loaders @joyeecheung

@targos targos added the esm Issues and PRs related to the ECMAScript Modules implementation. label Dec 16, 2024
@richardlau
Copy link
Member

Probably a duplicate of #56115 (fixed in 23.4.0 by #56122)?

@joyeecheung
Copy link
Member

Confirmed that it's fixed by #56122

@richardlau
Copy link
Member

For expectation management, the next Node.js 22 release is likely to be at the beginning of January.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

4 participants