Skip to main content

JavaScript Modules

Node.js will treat .cjs files as CommonJS modules and .mjs files as ECMAScript modules. It will treat .js files as whatever the default module system for the project is (which is CommonJS unless package.json defines "type": "module") [3].

In context of Node.js we can assume [2]:

Calling require() always use the CommonJS module loader.
Calling import() always use the ECMAScript module loader.

CommonJS Modules​

CommonJS modules are the original way to package JavaScript code for Node.js [2].

Usage example:

const circle = require("./circle.js");
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
circle.cjs or circle.js
const { PI } = Math;
exports.area = (r) => PI * r ** 2;
exports.circumference = (r) => 2 * PI * r;

Usage example:

const Circle = require("./circle-class.js");
const myCircle = new Circle(4);
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
circle-class.cjs or circle-class.js
const { PI } = Math;
// Assigning to exports will not modify module, must use module.exports
module.exports = class Circle {
constructor(r) {
this.r = r;
}

area() {
return PI * this.r ** 2;
}

circumference() {
return 2 * PI * this.r;
}
};

ECMAScript Modules​

ECMAScript modules are the official standard format [1] to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.

addTwo.mjs or addTwo.js
function addTwo(num) {
return num + 2;
}

export { addTwo };

Usage example:

import { addTwo } from "./addTwo.js";

console.log(addTwo(4)); // Prints: 6

Reading List​

Sources​

  1. https://tc39.es/ecma262/#sec-modules (2023.04.19)
  2. https://nodejs.org/api/modules.html (2023.04.19)
  3. https://stackoverflow.com/a/57492606/5842853 (2023.04.19)