News for hackers. Stuff that matters.
Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Thursday June 20, 2024 @02:44PM (#40741853)
Using dynamic import. Here's some documentation to get you started.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Thursday June 20, 2024 @06:57PM (#40744380)
Dynamic import() is asynchronous. I have things that are not asynchronous.

To rewrite to be asynchronous gets me exactly nothing.

In work that is asynchronous, I use import(). It's fine. Also, it gives me nothing but, since that particular thing is already async, why not?

This is a terrible, badly thought out thing that was done by ECMA that harms a ton of developers and that would have been entirely saved if somebody had asked me.

I would have said, add importSync() and there will be literally no problems ever again.

Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Thursday June 20, 2024 @07:08PM (#40744464)
No code or event handlers could run while importSync is resolving. I think including it would have been a mistake. The only other thing that's remotely similar are alert() and inputBox().

IMO it would be a pandora's box of unending problems. A function doing a synchronous dynamic import creates a bad user experience. It's also a pretty rare circumstance in my experience. Dynamic imports are relatively uncommon to begin with. Having one that must be inside a non-async function is unheard of for me.

If you had your way, I think the net result would have been worse.

Re:NPM and NodeJS should do more to make ES Modules easy to use by maple3142 on Thursday June 20, 2024 @08:09PM (#40744868)
I don't think it is a big problem given functions like fs.readFileSync already exists. It doesn't have to be a JS standard but be a server-side JS runtime specific function, which can be used when hacks are needed.
Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Thursday June 20, 2024 @08:29PM (#40744963)
Node could just do that. `fs` isn't an ES spec, and nor does `importSync` need to be. The blame can't be placed on ESM for this.
Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Wednesday June 26, 2024 @11:01AM (#40800849)
I agree. The blame belongs to the NodeJS steering committee. It's their failure and it's a big one.

The only excuse is that ESM happened during the big Deno Node wars when the community was split, everyone was mad and, apparently making dumb decisions.

I read that a new version of NodeJS has revised require() to bring in ESM modules. That's good enough. (I would prefer importSync() for clarity.)

Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Thursday June 20, 2024 @07:11PM (#40744501)
I understand that importSync() is blocking. That is fine when it is fine. I am a programmer. I can make sync vs async decisions.
Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Thursday June 20, 2024 @07:15PM (#40744533)
The browsers would never implement it.
Re:NPM and NodeJS should do more to make ES Modules easy to use by efilife on Friday June 21, 2024 @03:52AM (#40747180)
This is not a talk about browsers, hence the readFileSync examples
Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Friday June 21, 2024 @12:34PM (#40751216)
The reason we got here in the first place was trying to standardize the language and its module loading. If you come up with a solution that doesn't work in browsers, we're back to the same thing. Instead of the `require()` polyfill, we'd need the `importSync` polyfill.
Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Friday June 21, 2024 @11:34AM (#40750667)
Nor do they have require(). That’s not the point.
Re:NPM and NodeJS should do more to make ES Modules easy to use by recursive on Friday June 21, 2024 @12:27PM (#40751154)
And the fact that require() is non-standard is why we're talking about ESM now. Node can cook up another non-standard package loading thing whenever they want. It just won't be ESM. And if you don't care what browsers do, then we're not talking about ESM.