QUESTION: Что такое ES модули?
The modern JavaScript ecosystem is based on modularity standards, among which ES modules hold a special place. This is the official standard for organizing and reusing code in JavaScript, supported by both modern browsers and the Node.js environment. The main feature of this approach is that each file is treated as a separate, isolated module with its own scope. To share functionality with other parts of the application, developers use various types of exports. For example, named export allows exporting multiple variables or functions from a single file using a construct like export const x = 1, while default export uses the export default fn syntax, which is typically used for the main component or function of a module.
In turn, to include created modules in other parts of the program, import mechanisms are used. To get specific named items, destructuring is used at the time of import, for example, import {x} from './module'. If there is a need to import all of a module's functionality at once as a single object, star import is used: import * as M from './module', after which all exported entities become available as properties of the M object. A crucial characteristic of ES modules is static imports. This means that the dependency structure between files is determined and analyzed by the compiler even before the code itself executes, at the parsing stage. This approach allows build tools and browsers to understand in advance which files will be needed for the application to work.
For the browser to correctly process such modules when loading an HTML page directly, you must explicitly specify the script type in the tag. The type='module' attribute is used for this in HTML, for example, in the form of the script type='module' src='main.js' /script construct. Thanks to this, the browser enables strict module mode, automatically processes them as asynchronous scripts, and allows using all the advantages of modern import and export syntax directly on the client side of the web application without prior bundling.