Skip to the content ForoHackers
Log inJoin

Node 23 let require() load my module. Then I put a kettle on inside the module.

JavaScript and TypeScriptOpen sourceToolchains and linkers
#16373 · · Quote · Report

Node 23.0.0 is out. In my tiny local CommonJS program, require('./labels.mjs') gives me the exports from an ES module that just defines a label. I put a top-level await into a second copy and the exact same caller wants to go home. Does enabling experimental require(esm) by default entitle me to a synchronous cup of tea or did I misread the menu?

#16579 · · Quote · Report

For the record the synchronous version is export const label = 'hinge'. The second version has await Promise.resolve() before the export. I'm not waiting for the network. I can't even blame the kettle.

#16580 · · Quote · Report

The timing of your promise doesn't change the module's shape. The graph with top-level await is asynchronous. A synchronous require caller needs to get its result before it can continue. What does the error code say?

#16581 · · Quote · Report

ERR_REQUIRE_ASYNC_MODULE. The boring file works, the one with the immediately resolved promise doesn't. I expected it to inspect the promise, decide it was negligible, and otherwise run my life for me.

#16582 · · Quote · Report

Si la aplicación admite una operación asíncrona ahí, import('./labels.mjs') devuelve una promesa; espera el resultado en un lugar que pueda esperar. Si tu función pública promete devolver la etiqueta al instante, cambiarla en silencio por una promesa va a fastidiar a todos los que la usan.

#16583 · · Quote · Report

We once published a benchmark for "waiting zero seconds". I am delighted you have found the one kettle that makes our results look hasty.

#16584 · · Quote · Report

I moved the asynchronous choice up to the outer function and kept the instant little label file instant. Parent awaits the dynamic import of the async one. It's not my favourite answer, which is usually a sign that I've learnt something.

#16585 · · Quote · Report

And leave the actual version in the bug note. This is Node 23.0.0's experimental behaviour. Here it is in their words: https://nodejs.org/en/blog/release/v23.0.0

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply