ECMAScript Modules: ESM has three important features called imports, exports & dynamic imports
- It can export a subset of the full module
Exports: The export has two different types: default exports and named exports. One module can have many named exports but there is only single default export for every module. In ESM you export a binding a module and this binding has a direct connection with the exported value.
Re-Exports: The ES module has a special feature called “re-exporting” which can export the export of other modules.
Imports: Like exports, an import statement must be at the top level of a module. It is not possible to enclose it within a function. It provides you greater flexibility over what could be done with CJS/AMD and it negates a trend to generate the separate module.
Dynamic imports: The dynamic imports have features like import call for module loading. It is used to meet the requirements of asynchronous module loading in browsers. Dynamic imports are supported in Babel and TypeScript. It allows you to combine multiple dynamic imports and the result delivered by them is fairly intuitive code.
Node.js modules: It follows the CommonJS modules. The environment must have three variables: export, require and module.
- Export: A module exports values by assigning their value to the properties on the export object.
- Require: It imports other modules’ export using their module ids.
- Module: It provides metadata such as id and URI of the current module.