About
ECMAScript modules (ESM) are the official standard format for packaging JavaScript code for reuse. Modules are defined using a variety of import and export statements.
ESM are already the standard format for front-end development, and now you can use them on the back end with recent versions of Node.js. Although the old module system (CommonJS) has worked for many years, ESM comes with many benefits such as asynchronous loading, top-level await, and improved static analysis.
In this cheat sheet you will learn:
- How to enable ECMAScript modules
- How to import and export ECMAScript modules
Excerpt
Because Node.js treats code as CommonJS modules by default, module authors will need to tell Node.js to treat the code as ECMAScript Modules by either using the .mjs
file extension (as shown in earlier examples) or by using the type
field in the package.json file.