Skip to content

Latest commit

 

History

History
48 lines (31 loc) · 1.93 KB

10-modules.md

File metadata and controls

48 lines (31 loc) · 1.93 KB

🐢 Node.js

Modules (CJS & ESM)

In Node.js there are two ways to manage modules:

  • CJS for Common JavaScript
  • ESM for ECMAScript 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:

Articles related to ESM


⬅️ 🐢 Node.js: 🔍 Debugging & Profiling | ➡️ 🐢 Node.js: WebSocket