Using the ESM standard
CommonJS has been a good solution for Node.js, but not a desirable solution for JavaScript as a language. For instance, in the browser, CommonJS does not work. Doing synchronous imports on URLs is just not possible. The module resolution of CommonJS was also way too flexible in terms of adding extensions and trying directories.
To standardize modules in JavaScript, the ECMAScript Module (ESM) standard was established. It is capable of defining modules that run in the browser, as well as Node.js. Furthermore, instead of using an arbitrary function such as require
, the whole module system relies on language constructs using reserved words. This way, the module system can be brought over to the browser, too.
The ECMAScript standard specified two keywords for this:
import
: Used to import functionality from other modulesexport
: Used to declare the functionality that can be imported into other modules
The import
keyword must appear at the...