In Node.js there are two ways to manage modules:
ESM is the new standard integrated since ECMAScript 6 for module management. However, historically Node.js has been using CJS since the beginning and at the time there were a lot of concerns about the ESM specification (which made the implementation in the Node js ecosystem complex).
const { foo } = require("mymodule");
exports.foo = function boo() {
return foo();
}
With CJS it is necessary to use the require() function and keywords like module.exports while in ESM you can use the import and export keywords.
import { foo } from "mymodule";
export function boo() {
return foo();
}
ESM support on Node.js is becoming more and more stable and it is therefore not surprising that it will become the default module system in the near future.
I recommend you to listen to the following talks to better understand the subject:
- Extra Special Modules - Myles Borins, Google
- Keynote: The Future of JavaScript is Universal - Myles Borins, Developer Advocate, Google
- ES Modules in Node.js - Gil Tayar
Articles related to ESM
- Get Ready For ESM - Sindresorhus
- Pure ESM package - Sindresorhus
- Using ES Modules (ESM) in Node.js: A Practical Guide (Part 1
⬅️ 🐢 Node.js: 🔍 Debugging & Profiling | ➡️ 🐢 Node.js: WebSocket