Complexity is the enemy of development. ES Modules are the primary tool for conquering complexity by breaking code into isolated, reusable pieces.
1The Shareable Interface
Before modules, JavaScript relied on a single global scope, which led to naming collisions and hard-to-maintain code. ES Modules (ESM) provide a private scope for every file. To make a function or variable visible to the outside world, you must explicitly export it. Conversely, to use external code, you must import it using a relative path. This creates a clear 'contract' between files, making dependencies explicit and easy to trace.
2Named vs. Default Exports
There are two ways to export. Named Exports allow you to export multiple items from a single file, requiring the importer to use the exact name (unless aliased with as). Default Exports are used when a file has one primary purpose—like a single class or a main function. You can only have one default export per file, but it grants the importer the flexibility to name the import whatever they wish, simplifying the consumption of core library features.
