Require(esm) in Node.js
joyeecheung.github.io
joyeecheung.github.io
I really think the way ESM was handled hurt Node.js a lot. Couple this with the introduction of TypeScript and complexity in the Node.js ecosystem skyrocketed. I've lost so many hours on this.
Thankfully libraries like esbuild exist (and by extension `tsx` which uses esbuild under the hood). https://github.com/privatenumber/tsx
I remember the days of AMD, UMD and so on and so on. The complexity just got reduced during CommonJS days and then came back with a vengeance.
Must be something like that.
This never made a lick of sense.
(At the very least for synchronous modules.)
I created require-esm-in-cjs[1] two years ago when I needed to require ESM modules in a commonJS (regular legacy) page.
It is only a few bytes long and is simple - it effectively causes ESM modules to load synchronously by checking every 100ms if the JS has loaded before allowing the page to continue.
I have been meaning to improve it, such as adding a timeout and whatnot but never got around to it (it is good enough for my needs) - but always thought it was odd that something so (relatively) simple to hack around and so low in the stack can not be supported natively.
It gets a fair amount of downloads even though it doesn't get much visibility so I guess I am not the only one who felt the need. But native is MUCH better!
By the way, we've been saying this for years. And anyone who pointed out how ridiculous this was got ostracized by Node devs. It's just baffling how this was handled.
One has to wonder about the total number of hours wasted debugging issues that trace their origin to ECMA's decision.
AMD wasn't invented on a whim
Oh come on, overdue for what purpose? Digging Node out of another Node created hole?
Just look at the typing gymnastics in TypeScript.
Try better to avoid ad hominem attacks, please.
> but in the JS world there are band-aids everywhere
I apologize, "band-aid" was the wrong word to use for this specific case, because that unintentionally disparages "band-aids". You are right there are "band-aids" that fix real-world problems, and help heal accidental wounds. This experiment does neither and seems to me worse than a "band-aid". It's a fragile broken and fake bridge that looks like it can get you to your destination but will collapse right under you if you try to use it (rather than switching to what already works today).
?
It's so that CommonJS modules can use ESM modules.
---
Like suppose examplelib uses otherlib, both in CommonJS.
The examplelib wants to upgrade otherlib, but the new version uses ESM. Now, examplelib is stuck either on the old version, or has to undertake a rewrite (and propagate this situation downstream). Or adopt a transpiler and force downstream users to do the same.
Some would say "just rewrite in ESM" and that is a nice idea, but there are lots of tools with caveats, TypeScript, Jest, anything that depends on require-in-the-middle, etc.
It's hard to boil the ocean. (Just look at Python 3.)