ESM vs CommonJS in your Node Projects
HTML-код
- Опубликовано: 23 авг 2024
- In this video, we'll convert one of our TypeScript Express templates to use ES Modules instead of CommonJS and explain the differences between the two systems alo
GitHub Repo: github.com/cov...
Don't forget to like this video and subscribe to our channel - we're publishing more videos and walkthroughs every week. Comment below and let us know what you'd like to see next!
Join the Covalence community: covalence.io
Shop Covalence merch: covalence.merc...
THANK YOU! Watched the first minute of the video and you already helped solve by bug! God bless you :)
The link for the github repo in the description is empty. Can you please share it with us?
Sorry about that! Link shared
Hi, trying to get a bit more in this topic as I ignored it completely until now in my professional life. Could you include a view words about the pros and cons with using ESM or CommonJS? That's kind of what I expected from the titel. My current knowledge is only, that ESM supports treeshaking which could reduce your js package. That's why we're considering ESM over Common. But as stated, ESM has some "drawbacks" as the stated, "weird" import statements, less variables in node and so one.
Could you clear that topic up a little bit, either with a follow up or just in text form? Thanks already!
If Node were rebuilt today it would most likely be built using ESM first. The fact it was built originally using CommonJS is what causes any and all of the "drawbacks." My guess is that over time more and more NPM packages will choose to support ESM over CommonJS and some may even eventually phase it out altogether. Node may move in the same direction although I feel they'll be supporting both and creating tooling around both for a VERY long time. IMO the main benefit is really just consistency and potential interoperability but there are a few minor benefits having to do with import optimization and code size, but for 99.9% of backends this doesn't matter. At the end of the day, I honestly think deciding between the two is just a personal preference for your development team at this time.
@@covalence-io Can you link to something more like a udemy course or maybe longer youtube video that goes more in depth on this stuff? I can see what someone is doing on their screen and listen to what they're saying but a lot of times much of what is said goes over my head. I understand synchronous vs async for example, but what does that have to do with require and import statements? People say that CJS and EMS do them differently, but actually how and what is the tangible difference in outcome? No one really ever explains this, they just mention it briefly and expect you to understand. Along with this, there is a bunch of other things that are confusing as well. Tree-shaking, how exactly the target and module work in the typescript settings and why they are two different settings for what seem like what should be the same outcome, which is how node compiles the files... and honestly more I can't even think of. I need some kind of in depth course that will explain all these things and really take the time to show you the differences.
@@jamesalphonse1099 i would recommend checking out our courses at covalence.io. They may not specifically go into everything you mentioned above, but once you're in our community Discord channel, finding out more information about all of this should be easy.