Isn't ESM the "way forward" for Node.js? I've seen tons of posts a out how TypeScript allows use of modules, and that's always seen as a good thing, but then discussion of ESM always seems to be met with disdain.
Yes, ESM is the way forward. Server-side ESM is still a bit clunky. The gentle migration path is to dual build CommonJS + ESM. The node-fetch team took a hard stance of ESM-only, which breaks a lot of projects.
Node’s module management has been a clusterfuck and I get that there’s history behind it but damn how long have we have ESM in feature flags only for a half baked release, I hate the semantics of using .mjs or adding type module, and if all the planets align then node will read it as an external module
Can you care to explain how to fix it with node and typescript. I get told I'm importing it wrong, but it's the exact same way importing everything else.
If you use typescript you don't even have to do anything.
I've converted a few medium-sized Typescript projects from CJS to ESM.
Unfortunately, TypeScript still has some issues with Node & ESM. Notably, many popular CommonJS packages don't work with named imports such as import { foo } from 'bar'. You have to do import bar from 'bar' and then use it like bar.foo() or destructure it like const { foo } = bar. However, typescript won't tell you this at all. The broken import will not only compile fine, TypeScript will actually suggest it via auto-imports.
Also, modules in ESM require the .js extension - import foo from './bar' is invalid. But even if your project uses ESM, TypeScript's auto-imports will default to not using an extension. Again, it suggests and compiles code that immediately fails at runtime.
The second issue is apparently going to be improved in an upcoming release, but there's no solution in sight for the first issue.
Neither of these are the end of the world, but it means that converting a CommonJS codebase written in TS isn't as simple as changing a couple config flags, and the development experience post-conversion is noticeably more frustrating. And unless you want to use top-level await, you don't actually gain anything in exchange for this frustration. Except for the fact that some popular modules are dropping CJS support.
ESM may be the way forward, but at the moment it feels like a step backwards in terms of dev experience.
This was exactly the same mindset for Python2 to Python3. Honestly, it's a lot less painful if people just adopt the new standard, rather than continuing to borrow against the technical debt of staying on a platform that has officially been identified discussed as a potential target for deprecation (maybe).
Edit: clarified it is not official. The core Node team has not made an official statement as of this date.
Good call, it isn't official. Discussion can be found here. It's a hot topic, and there's a lot of mentions of libraries or frameworks ditching support for CJS, but Node.js has been neutral on the topic so far.
I don't even know how to get node fetch to work with express and typescript. I constantly get the error, had to go back to V2 or whatever it is that works.
62
u/Zipdox Feb 01 '22
Now we can finally ditch node-fetch, which I've wanted to do ever since it switched to ESM.