Module BasicsLesson 15Free preview
Node ES Modules
This lesson is part of Node.js · All levels
Summary
In addition to CommonJS, Node.js also supports ES Modules which are the modern standard for JavaScript. The ESM module is declared with import and export, and is active when the extended file .mjs or package.json sets type: module. ESM is evaluated statically so it's more friendly to modern tree-shaking and tooling.
Key points
- Activate the ESM with the .mjs extension or set type: module in package.json
- Use import to load and export to export
- ESM supports dynamics that return Promise
- The .cjs extension is used if you want to keep using CommonJS in an ESM project
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.