Thank god for competitor runtimes like Bun pushing this feature forward. It seems like the Node team has finally decided to ACTUALLY work on something useful-without the competition, we probably wouldn’t have seen this from them anytime soon.
@fred.flintstone40993 ай бұрын
Yeah, and Deno too. Bun and Deno are so much faster and easier to use. Deno is also more secure. I think Node.js should just recommend everyone to ditch it and upgrade to Bun or Deno instead.
@ivanjermakov3 ай бұрын
Am I the only one who completely dropped CJS from all my projects back in like 2020? There is literally no reason to hold onto the old way of doing it, since the great ES6 support on all platforms.
@HawkSerj3 ай бұрын
Yes, you are
@RaVq913 ай бұрын
anybody who cares about tree shaking did the same thing (even earlier)
@Sv443_3 ай бұрын
For new projects, sure, but maintaining and updating old projects is a pain without ESM compat.
@SirusStarTV3 ай бұрын
React for example doesn't seem to have .mjs files or any ESM syntax in them. Even though in the examples they provide they use import statements loading cjs files.
@zettca3 ай бұрын
@@SirusStarTV the React team basically said they're only moving to ESM after everyone else does
@thomasini3 ай бұрын
Personally I think the most common error in all of JavaScript is the worst one: Cannot read property "someMethod" of undefined. Why can't JavaScript (or node) say instead: Cannot read property "someMethod" on "config" because it's undefined? That would be a much nicer error message IMO.
@Pazuno3 ай бұрын
Everyone switch to ESM, then problem solved, this fragmented module system is madness
@SirusStarTV3 ай бұрын
JSR should be used. Only ESM modules allowed.
@corey444827 күн бұрын
Have you worked with large codebases?
@maelstrom5727 күн бұрын
@@corey4448 Yeah. So what's the problem with ESM and a large code base?
@corey444827 күн бұрын
@@maelstrom57 problem is that you cannot easily migrate large codebase from commonjs to esm due to various reasons, from complexity to usage of commonjs-only dependencies
@maelstrom5727 күн бұрын
@@corey4448 So the problem isn't the size of the code base; it's the fact that you depend on CJS-only dependencies. Most packages of this type are deprecated anyway so you're gonna have a problem even if you stick to CJS forever.
@ColinRichardson3 ай бұрын
I love how we know, if everything goes terribly wrong, you could safely get a job directing aircrafts with those little red sticks.. Your arm movements were NEXT LEVEL in this video.. Keep up the great work Matt!
@BlackAsLight4483 ай бұрын
Node is trying to catch up on what other runtimes have already achieved
@fred.flintstone40993 ай бұрын
Yeah, Bun and Deno are way ahead, easier to use and faster.
@maziasty3 ай бұрын
Props to node.
@gordonjohnston83213 ай бұрын
The only other system I can recall that used ‘odd number versions are less stable’ numbering was the Linux kernel back in the day. For example 2.2 and 2.4 were stable versions and 2.1 and 2.3 were development versions. However they stopped doing that with 2.6
@EdwinMartin3 ай бұрын
Ubuntu stil does this
@jtw-r3 ай бұрын
Love the wooden bookshelves in the background! Very nice homely touch
@dasten1233 ай бұрын
Great now we just need to wait one or two years until you can use it in production. Or switch to Bun today.
@fred.flintstone40993 ай бұрын
Or Deno.
@AutisticThinker3 ай бұрын
3:39 - The Linux Kernel does this; a few others that I don’t recall atm too. This is very old school.
@weirdwordcombo3 ай бұрын
on another note: i think a painful thing about the JS module system is that the module paths depend on what the filesystem paths are (at least when using relative paths). whereas for example in C# they are completely independent, so you can move and rename files without the import paths having to change at all. this is such a freaking pain because the tooling is never perfect and you always have to correct a path manually when doing massive refactors.
@dazraf3 ай бұрын
After more than 25 years Javascript dev is still a mess. Thank goodness we have people such as you to guide us through the forest.
@kyrregjerstad3 ай бұрын
how did you do that little magic file-switching trick at 0:24? :O
@felixgraphx3 ай бұрын
Ctrl+ a number switches tabs. E.g. ctrl+1 or ctrl+2 , etc...
@Novascrub3 ай бұрын
the even/odd release/dev pattern started with the kernel during the v2 era, iirc
@randyt7003 ай бұрын
Why are there efforts/resources being spent on maintaining two module systems? Shouldn't the community and tooling just move forward with ESM?
@mattpocockuk3 ай бұрын
Yep, we're trying
@kupopo13 ай бұрын
It turns out adoption problems are Really Hard. Imagine you maintain a common library. If you migrate to ESM then you're stranding any users who haven't themselves migrated. Not to mention the fact that a large share of codebases aren't even actively maintained.
@ajv70383 ай бұрын
Would this support be available for building web apps with some dependencies no longer supporting common js imports in new versions?
@Gakai083 ай бұрын
Hi Matt, thanks for the great updates on stuff like this. One quick question: Is there a reason to not use --experimental-transform-types instead of --experimental-strip-types? To me it just sounds like the superior version as it implies the strip-types flag too :o
@mattpocockuk3 ай бұрын
I just really hate enums 😂 But yes it's better
@Gakai083 ай бұрын
@@mattpocockuk haha yes, I also don't like enums, but namespaces can occasionally be useful, as you mentioned in the react props pattern video.
@mattpocockuk3 ай бұрын
@@Gakai08 Yes, though you can use 'declare namespace' for those which dodges any runtime behaviour.
@satanacchio3 ай бұрын
--exoerimental-transform-types enable sourcemaps while --experimental-strip-types does not. So there is a performance advantage
@ashuvssut233 ай бұрын
Whats the situation in Deno for this? can we use esm in cjs in deno?
@SirusStarTV3 ай бұрын
When we want to include node modules in our js bundle with import statements like import { foo } from "./bar.js"; How can bundler like webpack know if we need to bundle "bar.js" or just leave these statement as is because browsers support imporing ESM modules? Only absolute urls are fixed so that no one should interpret it in different way.
@mattpocockuk3 ай бұрын
Webpack won't leave any files as they are, not sure this is a real problem.
@Yin1173 ай бұрын
I adore your content, thanks for keeping me upto date 😀
@zx-sy1qh3 ай бұрын
This has taken too long in the oven to fix for node! Already have baked buns!
@masterflitzer3 ай бұрын
one should move slowly to esm modules, so legacy commonjs should be importable into esm, but importing esm into commonjs is just unneeded and people will continue to use commonjs that way
@DefekacjaOdbyta3 ай бұрын
Sure, if you’re starting from scratch, but if you have a legacy system that you can’t migrate to ESM just like that, then having the ability to import ESM from CJS can make the gradual migration easier.
@edgeeffect3 ай бұрын
The gradual migration has been going on for... ... ... how many years now?!
@masterflitzer3 ай бұрын
@@edgeeffect that's the problem, but i doubt this change will speed it up
@lemontec3 ай бұрын
ESM packages in Expo Router APIs here I come!
@mzhomie88803 ай бұрын
Deno 2.0 ❤
@samislam27463 ай бұрын
❤❤
@Caldaron3 ай бұрын
❤❤❤
@Muhammed-zn7ft3 ай бұрын
❤❤❤❤
@fred.flintstone40993 ай бұрын
Yeah, I think Node.js is pretty awful and this Deno and Bun seems much nicer.
@robertsandiford62233 ай бұрын
I think everyone knew this was needed, ever since ESM, didnt they?
@vinception7773 ай бұрын
God I love your videos, they are always super insightful, well explained and peaceful, thanks for sharing Matt☺🙏
@renegadeace17353 ай бұрын
I was just dealing with this today lol
@MinaLuke27 күн бұрын
Is it only me who think allowing esm imports in commonjs files with such simplicity would delay the migration from cjs to esm 😅. With top level await is the only obstacle i see devs and libs maintainers would delay the conversion to esm even more.
@bordeux3 ай бұрын
ufff i do not care about it anymore with Bun :) It just works there.
@ra2enjoyer7083 ай бұрын
There is a lot of wrong information in this video, I'd attribute it to not being informed enough, but for sure the guy who deep dives into experimental typescript features wouldn't use NodeJS/Javascript and Typescript interchangeably when describing features, so I'd go over them one-by-one: - 0:08 - This is not an "error", but the result of community treating ESM as some sort of interchangeable syntactic sugar over CJS. Turns out when you have to interop a strict module system (ESM) with a lax one (CJS), you'd have to have pretty strict rules since you don't have a bundler luxury of infinite build time to make assumptions about modules so they would "just work" with any infinite input configurations. - 0:31 - There is no such thing as "CJS" in "Javascript". CJS module system exists purely in NodeJS and people (and a lot of tooling) assume it's javascript because of its close association with frontend development pipeline. Even ESM semantics differs between ESM in NodeJS and ESM in browsers. `.mts`/`.cts` also has nothing to do with nodejs or ESM, it's purely a typescript construct, which doesn't even provide guidelines on how you should tell them apart in `.jsx`/`.tsx` files. I assume it's `.mtsx`/`.ctsx` but good luck doing that without typescript/bundlers (especially "no config" ones) chocking on them. - 1:02 - It's quite an overstatement, this flag merely allows to write a subset of Typescript without a "build step". Naturally that means it will bring problems down the line when this code will have to interact with code which was written and distributed as typescript with a build step. - 1:40 - It's not so much about `await import()` (aka dynamic import) but ESM modules resolving asynchronously, unlike synchronous-resolving CJS modules. The example can be rewritten as promise chain without changing the behaviour. - 3:43 - It's not too funky, NodeJS gets away with it because it tells about this versioning scheme on its download page and structures said page accordingly. It not being a random lib which you can easily install off `npm` helps with avoiding the confusion also. In regards to top level `await`, I'd advice not using it even if your output environment supports this construct. It can easily kill any benefits of async resolution of ESM (by effectively forcing a sync resolution order on module graph) if not used carefully. It's only safe to use in "leaf" modules, aka modules which do not export any symbols, so things like the files you feed into `node` command line.
@mattpocockuk3 ай бұрын
- Yes, it's an error. ERR_REQUIRE_ESM, to be precise. - Yes, it's .mtsx and .ctsx. - Yes, how dare I simplify it down into a sentence. Also, I'm not saying you should distribute packages in TypeScript, that feels like a hallucination on your part. - I feel confident that my audience knows that async/await and .then are the same thing. - It's funky. Agree on TLA.
@Pete1333 ай бұрын
How incredible for this commenter to be so confidently wrong 🙃
@ra2enjoyer7083 ай бұрын
@@mattpocockuk - Loading a non-existent module is also technically an "error" but it's not the one nodejs can fix by rolling a new version. - Need a source on that. The only source about these extensions being real is your site, they aren't mentioned in typescript docs and were only brought as a talking point during a design meeting in typescript repo with no apparent resolution. - "writing typescript without typescript" implies that you can freely consume and distribute such code on npm.
@mattpocockuk3 ай бұрын
@@ra2enjoyer708 - I don't know what point you're making here. - The source is I just tried it and it works. - No it doesn't.
@duytdl3 ай бұрын
Finally! Also to anyone lamenting CJS, it's not about moving on, it's about maintaining backward compatibility. I'm guessing y'all are noobs and only started coding a year ago. When you'll have a gazillion old little utils and modules, you'll understand the significance of not wasting 6 months every 6 months just keep upgrading all your code.
@Derrickasaur3 ай бұрын
Additionally, backwards compatibility is in tune with the JavaScript ethos It's always been the #1 rule of the web and it's what makes JavaScript a great language - the CJS/ES6 divide is unnecessary if the two modules can officially co-exist (they do unofficially anyway, since there is generally a workaround for using one or the other or some combination thereof)
@ordazgustavo3 ай бұрын
Ubuntu also uses odd numbers for non LTS releases
@mattpocockuk3 ай бұрын
Good to know!
@rawallon3 ай бұрын
Node 23 has the sauce
@Iammrunkown3 ай бұрын
Use Deno
@fred.flintstone40993 ай бұрын
Yeah, or Bun. Both Deno and Bun seems much better than Node.js.
@Iammrunkown3 ай бұрын
@@fred.flintstone4099 Bun sucks as it does not even support Node properly. Next.js does not work without installing Node. Whereas Deno is secure by default, has built-in browser APIs, is faster than Node, has a formatter and a linter, has its own JavaScript registry, has zero build-time, supports Node completely, and so on.
@danielsharp24023 ай бұрын
At least for me most of the friction with ESM is that commonjs "just works", you can import without the stupid looking .js extensions in typescript, index exports got fucked because someone thought it's a good idea. CJS just works as expected with the interop flags people are used to in tsc. The moment I try to use new module resolution etc errors keep popping up. This is why CJS is still used because ESM is a big old mess.
@ra2enjoyer7083 ай бұрын
This sounds like a typescript (and bundler) problem more than anything. Also you've got it all backwards - It's CJS being a giant mess which was quietly sweeped under the rug. I strongly doubt you've written a lot "CJS" code to have any strong opinion on it. Chances are it's merely a buzzword somewhere in your build config file and all your input code is just a microsoft approved ESM-like syntax which doesn't rely on a single "feature" of CJS.
@nomadshiba3 ай бұрын
using esm in commonjs shouldnt be supported. it should be one way. this update will only make lifetime of commonjs longer
@bigmistqke3 ай бұрын
Wrong. It is one of the reasons so many libraries are still published in commonjs. Anything that helps migration is a good thing.
@edgeeffect3 ай бұрын
Deno 2.0 fixes the worst things about Node. ;)
@mo3k3 ай бұрын
"fixes the worst thing about JavaScript" is a very clickbait title considering this has nothing to do with JavaScript, it's a node thing AND it's no where near "the worst". Thx for the video though.
@mattpocockuk3 ай бұрын
The ESM/CJS divide is genuinely one of the most divisive things about JS. Node is EVERYWHERE in the JS ecosystem, and improvements to it ripple out to a huge part of the JS world.
@dada786413 ай бұрын
It's insane to me they didn't launch modules with this feature. As is the module transition was Node's version of Python 2 to 3, and now years later they're like "lol jk we're making it painless anyway!" By far most libraries don't use top level await because it's a bad practice anyway (unless you really need it for some reason).
@dada786413 ай бұрын
But then the import/export syntax itself was inconsistent and confusing in its first iteration so I guess the whole thing was just needlessly ill conceived from the start.
@MrMudbill3 ай бұрын
I refuse to believe there are any sane people left using CommonJS
@twilightdev3 ай бұрын
So odd version of node has odd behavior
@cryptixdaemon72363 ай бұрын
Fake it till you make it, that's JS
@chrisstroud19153 ай бұрын
I read the thumbnail as ”Worst Burger" and was so confused
@samislam27463 ай бұрын
Finally!!!!
@danser_theplayer013 ай бұрын
Just ctrl + F, match by regex, replace all require() with import() and stop using common js.
@rafadydkiemmacha75433 ай бұрын
It's brave to call this a game changer. Pretty sure 99% of Node users don't give a damn about this anymore.
@mattpocockuk3 ай бұрын
Definitely a game changer for folks who ship libraries.
@mattpocockuk3 ай бұрын
And TBH, anyone using Jest.
@erics21333 ай бұрын
@@mattpocockuk That was one of the side benefits of my dropping Jest for vitest.
@PieterWigboldus3 ай бұрын
A bug? For me it sounds like a feature that wasnt build. The new chance wil make it a lot easier for ESM packages that dont have to generate a CJS version anymore.
@SolmanNurov3 ай бұрын
finally
@akam99193 ай бұрын
I fail to see why they can't just block the event loop for any top level awaits. Is there *really* that meaningful a difference? Perhaps a future flag for handling top-level awaits when importing esm in cjs. Perhaps even an future where one can customize how one "requires" a module, regardless of it being esm. Though, maybe the real fix is updating commonjs spec to just allow top-level awaits. This is a very arbitrary decision to begin with that would have very consequences (assuming it treats a top-level await in an module the same way that esm does).
@Patterner3 ай бұрын
"cjs is old, use mjs" node developers [citiation needed]
@kilo.ironblossom3 ай бұрын
Ohh!! This bloated ecosystem again 😂
@shapelessed3 ай бұрын
Still better than that of C++
@abcq13 ай бұрын
With this title I expect to hear that Node.js 23 erased Typescript from history and executed its inventors. Lets see.
@edgeeffect3 ай бұрын
Back in the long ago, I was in the process of migrating from C++ to Ada... and I was all "oh, I love how this strong typing prevents me from making so many errors and leads to more explicit/readable/maintainable code" then in jumped Perl, Python and Ruby with their "let's throw away types enttirely"... well, When Python, TypeScript and PHP started putting the types back in, I threw a massive metaphorical "welcome back home typesystems" party.
@abcq13 ай бұрын
@@edgeeffectTypes are cool in places when they are appropriate, like C++ or Rust. When the comiler needs to know ahead of time how much memory to allocate and how to handle it. But not in JavaSscript. Typed JavaScript is like C++ without types. Nonsense.
@recursiv3 ай бұрын
@@abcq1 Types are for people too.
@edgeeffect3 ай бұрын
@@abcq1 that's half of the purpose of types, yes... but only half.