CommonJs, AMD, CMD, ES6 are specifications used for modular definitions, aimed at standardizing the introduction of modules, handling of dependencies between modules, solving naming conflicts, and using modular solutions to decompose complex systems into more reasonable code structures with higher maintainability.
CommonJS is the specification for NodeJs server-side modules. According to this specification, each file is a module with its own scope. Variables, functions, and classes defined within a file are private and not visible to other files. The module variable inside each module represents the current module. This variable is an object, and its exports property is the external interface. Loading a module actually means loading its exports property. In summary, the CommonJS specification uses require for importing and module.exports or exports for exporting.
It's also possible to use exports for exporting, but never overwrite the reference of exports because exports is just a pointer that points to the memory area of module.exports. Therefore, overwriting the exports changes the pointer and renders the module unable to be exported. In simple terms, exports provides a convenient way for writing, but it all comes down to using module.exports for exporting. Additionally, if both module.exports and exports are used in a file, only the content of module.exports will be exported.
AMD stands for Asynchronous Module Definition and is a modularization solution for the browser. While the CommonJS specification introduces modules with synchronous loading, which is not an issue for the server-side as modules are stored on the disk and can wait for synchronous loading to complete, in the browser, modules are loaded via the network. Synchronous blocking loading of modules in the browser could lead to the browser's page becoming unresponsive. AMD solves this by loading modules asynchronously, where the loading of a module does not affect the execution of subsequent statements. All statements depending on this module are defined within a callback function, which will only run after the module is loaded. RequireJS is an implementation of the AMD specification.
CMD stands for Common Module Definition and is SeaJS' standardized output in the promotion process. It is also a modular asynchronous solution for the browser. The main differences between CMD and AMD are:
AMD is executed in advance (relative to the definition callback function, AMD loaders pre-load and invoke all dependencies before executing the callback function), while CMD is executed with delay (relative to the definition callback function, CMD loaders load all dependencies and then execute the callback function. When needing a dependent module, it is then called upon to be loaded and returned to the callback function). However, starting from RequireJS 2.0, it can also be delayed.AMD has dependency preloading (the dependent modules are declared at the time of defining the module), while CMD has dependency proximity (the modules are only required when they are used - on-demand loading, meaning they are loaded and returned to the callback function when needed).At the language standard level, ES6 implements module functionality and is designed to be a universal module solution for browsers and servers. The ES6 standard uses export and export default for exporting modules and import for importing them. Additionally, in the browser environment, it is possible to use require to import modules exported using export and export default, but it is still recommended to use the import standard for importing modules.
A few key differences between export and export default are:
export allows for selective imports, while export default does not.exports are possible, but there can only be one export default.export can directly export variable expressions, while export default cannot.export, curly braces {} are required during import, while export default does not require them.