Imagine having all your Javascript code messed in just one giant file. Reading thousands of lines trying to find that typo that is making your app not compile doesn't sound like a good plan.
Thanks God (or whatever), Javascript acquired simple ways of implementing modularity with all the benefits that importing and exporting bunches of code implies.
Nowadays, in terms of modularity, most popular patterns are CJS, AMD, UMD and ESM and the goal of this post is covering each of them at a high-level so you could make choose the one that meets your needs better while building your library.
Since it depends a lot on your needs, ESM looks like the best modularity pattern option thanks to its simple syntax, multiple platforms support and asynchronously and tree-shaking abilities.
UMD works also in all platforms and is usually used as a fallback in case ESM does not meet your needs. Many popular libraries support this format for example moment.js and lodash.
In case you need detailed information, there is an amazing article with the whole history of javascript modularity on Github.
Thanks a lot for reading! Remember this post is part of “From Zero to Hero: Painless way of building a great Javascript library” series.