3 ms·
Why does it matter that people use ES modules instead of requires? They’re compatible enough. Javascript is weird because it has this directive for browsers to
by righthand 2y ago
Why does it matter that people use ES modules instead of requires? They’re compatible enough. Javascript is weird because it has this directive for browsers to keep compatibility, but then has proponents for language changes that people try to force on everyone through framework/library use and design. All to the benefit of someone reading code the way they want to read code.
It hasn’t changed because it’s not a real problem. This is like forcing main as the default branch in git.
- rty32 2y agoWell, to begin with, they are NOT compatible, in the general sense. What you perceive to be compatible works because of tons of (often dirty and ugly) hacks people put together, with many issues. (Hint: top-level await). Do your research.
- righthand 2y agoHence the “enough” modifier. NodeJS isnt a browser framework, do your research.
- tqwhite 2y agoI could not agree more. In fact, the introduction of ESM has been entirely pernicious, causing more problems, not fewer. Why? Because there is no importSync(). If that existed, it would be easy to interoperate.
- deleted 2y ago[deleted]
- tracker1 2y agoI think it would have been more prudent if Node had chosen to stay closer to Babel compatibility in terms of how it handled ESM + Require. I know there were reasons to break things... but it could have been smoother.
- spartanatreyu 2y agoWhy do you want an `importSync()`? You could achieve the same thing with a bunch of `import()` calls, but I'm not sure why you'd want to when you can just use the `import` keyword instead.
- tqwhite 2y agoI have a ton of projects and a huge amount of utilities and such, that all use CJS. You cannot just use 'import' if you also use CJS. To use 'import', it must be an ESM module and the you cannot use require() to access CJS utilities. In CJS code, one can use import() to bring in ESM modules but, import() is asynchronous. The problem is that I have a lot of code that is not asynchronous. To add an asynchronous function requires substantial rewriting that I am rarely willing to do. If, in addition to the asynchronous import(), there was a synchronous importSync(), I could use ESM modules any time I want. That would allow me to... 1) Use and support new utilities that are ESM. 2) Write new utilities that are ESM. Presently, I always target CJS because that's what I have to use. I have read that there is a new version of NodeJS that supports using require() for ESM modules. That will be a huge, huge, huge improvement although I would prefer importSync() so that it was clear when I was using an ESM module. Even so, I will take it.