News for hackers. Stuff that matters.
Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Thursday June 20, 2024 @11:32AM (#40739878)
Really? I have a project that loads modules based on calculated values. How will you do that in a pure ESM world?
Re:NPM and NodeJS should do more to make ES Modules easy to use by zarathustreal on Thursday June 20, 2024 @11:56AM (#40740170)
You wouldn’t, because it’s a misunderstanding of what “module” means and how they work. It’s like saying “maybe I need this file” - there is no maybe, you either need it (to run conditional logic upon) or you don’t.

You get an approximation of conditionally loading a module by simply converting everything to ESM and enabling tree-shaking. When module loading has no side effects this is simple to do. If you’re unsure at build time whether you will call a certain function from a certain module and that’s the reason you’re conditionally loading it, again enable ESM and tree shaking and import the function and call it conditionally except do so at runtime. You’ll only get exactly what you need to avoid the bloat of the whole module and your dependency graph will also be more correct as a bonus!

Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Thursday June 20, 2024 @06:53PM (#40744343)
You are simply wrong.

I've been a full time NodeJS programmer for fourteen years. Module isn't some pure theoretical concept. It is a file that exports something in Node.

There's a ton of 'maybe I need this file' in the world. For example, I have things that are polymorphic by instance. I grab a config entry to decide which of several modules are going to be used. The others are completely unnecessary.

You might say, just add a build step that chooses the right ones. To which I would say, Are you nuts? Why would I screw up a perfectly working paradigm?

To get "an approximation of conditionally loading".

Re:NPM and NodeJS should do more to make ES Modules easy to use by zarathustreal on Thursday June 20, 2024 @09:07PM (#40745173)
As a matter of fact _module_ is a pure theoretical concept. The fact that we’re discussing at least two different implementations of modules alone proves that it’s at least more abstract than _file_. It’s certainly not strictly related to Node, plenty of languages (and their associated runtimes, if applicable) have modules. Further, it wouldn’t matter what a specific implementation of modules within Node is, nor would 14 years of experience with Node.

You’re missing the point entirely, perhaps intentionally? If you’re using modules you’re already using a “build step” - code that runs before your actual code. Your “perfectly working paradigm” is only such if you intentionally disregard the core reason for modules: modularity.

Re:NPM and NodeJS should do more to make ES Modules easy to use by efilife on Friday June 21, 2024 @03:54AM (#40747190)
> code that runs before your actual code But this is not a build step. A build step would transform the code beforehand
Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Friday June 21, 2024 @11:41AM (#40750724)
For good measure, it is an operation that executes by the runtime for every execution, not as a redeployment.

Turns out that the ESM purists struggle to find reasons to condemn those of us who don’t have the need or resources to the chase bleeding edge of purity.

Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Friday June 21, 2024 @11:38AM (#40750699)
Your point is that modules has a conceptual underpinning. My point is that the actual world in not the same as that concept.

I use modules for all the reasons. Your assertion that require() or importSync() represents a willful misunderstanding is silly and arrogant.

Re:NPM and NodeJS should do more to make ES Modules easy to use by zarathustreal on Friday June 21, 2024 @09:34PM (#40755463)
My assertion is that, having been made aware of the pitfalls of effectful imports, continuing to argue for using them requires a willful misunderstanding.

People outside the context of this discussion could simply be uninformed, they don’t need to be willfully misunderstanding.

Re:NPM and NodeJS should do more to make ES Modules easy to use by tqwhite on Wednesday June 26, 2024 @11:06AM (#40800907)
No. It is a result of a practical, engineering perspective. I love theory. It's necessary for moving the world forward.

I also love getting things to work. Three quarters of NPM packages are CJS. Several of them are ones I wrote.

The reason they are not ESM is that the theoretically pure folks had control and I voted with my fingers because everything I do has to fit in my a substantial amount of existing CJS code.

Arguing for practical solutions that accommodate an imperfect world is absolutely correct.

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.