Modules

In the early days of JavaScript, files were small. You might have a single script.js file powering a few dynamic interactions on a static HTML page.

Today, JavaScript powers massive, full-fledged applications. Putting all your logic, state management, and API calls into a single file would be an unmaintainable nightmare.

Modules are JavaScript’s mechanism for splitting code into separate, isolated files, allowing you to import and export functionality across a large codebase as needed. This is identical to “packages” in Go or “modules” in Python.

In JavaScript, modules exist precisely at the file level. One file equals one module.

The Evolution of Modules

Because JavaScript was originally built only for the browser, it did not initially have a built-in module system.

Over the years, the community invented their own module systems (like CommonJS for Node.js). Eventually, an official standard was added directly into the JavaScript language specification, known as ES6 Modules.

As a modern developer, you must understand both.