Create a file called math.js:
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };In app.js, import the module using require():
const math = require("./math");
console.log(math.add(5, 3));
console.log(math.subtract(10, 4)); const math = require("./math");
console.log(math.add(5, 3));
console.log(math.subtract(10, 4)); const math = require("./math");
console.log(math.add(5, 3));
console.log(math.subtract(10, 4)); module.exports = function (name) {
return `Hello, ${name}!`;
};module.exports = function (name) {
return `Hello, ${name}!`;
};module.exports = function (name) {
return `Hello, ${name}!`;
};Importing a single exported function:
const greet = require("./greet");
console.log(greet("Alice")); const greet = require("./greet");
console.log(greet("Alice")); const greet = require("./greet");
console.log(greet("Alice")); ES6 Modules are the modern way to work with modules in JavaScript. They use export and import and work in both browsers and Node.js (with "type": "module" in package.json).
Create math.js:
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}Only one default export per file:
export default function multiply(a, b) {
return a * b;
}export default function multiply(a, b) {
return a * b;
}export default function multiply(a, b) {
return a * b;
}In app.js:
import { add, subtract } from "./math.js";
console.log(add(5, 3));
console.log(subtract(10, 4)); import { add, subtract } from "./math.js";
console.log(add(5, 3));
console.log(subtract(10, 4)); import { add, subtract } from "./math.js";
console.log(add(5, 3));
console.log(subtract(10, 4)); import multiply from "./math.js";
console.log(multiply(4, 5));
import multiply from "./math.js";
console.log(multiply(4, 5));
import multiply from "./math.js";
console.log(multiply(4, 5));
import * as math from "./math.js";
console.log(math.add(2, 3));
console.log(math.subtract(7, 2));
import * as math from "./math.js";
console.log(math.add(2, 3));
console.log(math.subtract(7, 2));
import * as math from "./math.js";
console.log(math.add(2, 3));
console.log(math.subtract(7, 2));
To use ES6 modules in Node.js, add "type": "module" in package.json:
Then, use import instead of require().
import { add } from "./math.js";
console.log(add(10, 5));import { add } from "./math.js";
console.log(add(10, 5));import { add } from "./math.js";
console.log(add(10, 5));FeatureCommonJS (require)ES6 Modules (import)Default inNode.jsModern JavaScript (ES6)Syntaxrequire() / module.exportsimport / exportLoad TypeSynchronousAsynchronousCan be used in BrowsersNo (without bundlers)Yes (Supported in modern browsers)
Use CommonJS for Node.js applications that do not require modern JavaScript syntax.
Use ES6 Modules for modern JavaScript projects, browsers, and future-proof development.
Modules improve code reusability and organization. ES6 Modules (import/export) are the standard for modern JavaScript, while CommonJS (require/module.exports) is still widely used in Node.js. The next section will cover working with databases in JavaScript, focusing on connecting Node.js to relational and NoSQL databases.