Module Export & Require
Splitting code into multiple files keeps a Node.js project organized and maintainable. In this chapter, we'll explore how to share functionality between files using CommonJS (CJS) and ES Modules (ESM).
CommonJS Modules (CJS)
Problem
You have two files : app.js and xyz.js— and want them to interact. How can app.js execute code or use functions from xyz.js?
Solution
Use require() to load one module into another and module.exports to expose functionality.
1. Requiring a Module
Running node app.js logs:
2. Exporting a Single Function
const sum = require('./sum'); // Load the sum module
console.log("Sum:", sum(5, 3)); // Sum: 8
3. Exporting Multiple Items
const x = "Exported value";
function calculateSum(a, b) { return a + b; }
module.exports = { x, calculateSum };
const { x, calculateSum } = require('./sum');
console.log(x); // Exported value
console.log(calculateSum(10, 5)); // 15
4. Destructuring Imports (Common Pattern)
This cleanly pulls out only what you need.
5. Folder Modules (index.js)
Structure:
const sum = require('./sum');
const multiply = require('./multiply');
module.exports = { sum, multiply };
const { sum, multiply } = require('./utils');
console.log(sum(2, 3)); // 5
console.log(multiply(2, 3)); // 6
6. Requiring JSON & Built‑in Modules
const config = require('./data.json');
console.log(config.name); // MyApp
const fs = require('fs');
const path = require('path');
ES Modules (ESM)
ESM is the modern JavaScript module system. To enable it in Node.js, add "type": "module" to your package.json or use the .mjs extension.
ESM Module
- Newer version of Node.
1. Exporting
export const PI = 3.14159;
export function add(a, b) { return a + b; }
export default 2.718;
2. Importing
import E, { PI, add } from './mathUtils.js';
console.log(PI); // 3.14159
console.log(add(2,3)); // 5
console.log(E); // 2.718
Key Differences: CJS vs. ESM
| Feature | CommonJS (CJS) | ES Modules (ESM) |
|---|---|---|
| Loading | Synchronous: require() blocks until the module is loaded |
Asynchronous: import can load in parallel without blocking |
| Strict Mode | Non-strict by default | Always strict—enforces better parsing and error handling |
Overall, ES Modules offer non‑blocking loading and built‑in strict mode, making your code more efficient and reliable.
Additional Patterns
- Alternative CJS Exports
- Import All
- Default-only Export
Recap
- CommonJS uses
require()andmodule.exports. - ES Modules use
importandexport. - Organize with folders,
index.js, or JSON imports. - Choose CJS for legacy, ESM for modern codebases.