ES6 modules
ES6 is the first time JavaScript has built in modules.
The goal of ES6 modules was to create a format that both users of CommonJS and of AMD are happy with. Its syntax is even more compact than CommonJS. Their structure can be statically analyzed. Their support of cyclic dependencies is better than CommonJS. Similar to AMD, they have direct support for asynchronous loading and configurable module loading.
Use of native JavaScript modules is dependent on the import
and export
statements.
To get modules to work correctly in a browser, you need to make sure that your server is serving them with a Content-Type
header that contains a JavaScript
MIME type such as text/javascript
Named Export
With named exports, one can have multiple named exports per file. The name of the imported module has to be the same as the name of the exported module.
Default Export
There can only be one default export per file. The naming of import is completely independent in default export and we can use any name we like.
We can have both named exports and default export in the same module.
Renaming imports
If 2 modules import names collide we would need to rename when importing.
Renaming exports
Renaming exports are similar to renaming exports. If you want to export the same value under different names.