0Pricing
Node.js Backend Development Bootcamp · Lesson

Node.js Module System Explained

Explore CommonJS modules, ES modules, and how to import and export functionality across files in Node.js.

Node.js Module System Explained is a free Node.js Backend Development Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Node.js Backend Development Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Organize Your Code with Modules

As apps grow, one giant file gets messy. Modules split code into small, reusable, focused pieces. Node supports two systems: CommonJS and ES Modules.

CommonJS: `require()` & `module.exports`

CommonJS is Node's original module system: require() pulls code in, module.exports pushes code out. Import what you need, export what you offer.

CommonJS: Exporting a Function

Here's mathUtils.js. Whatever you assign to module.exports is what other files can import from it — here, just the add function.

const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = add; // Only 'add' is exported

CommonJS: Importing and Using

In another file, require('./mathUtils.js') pulls in that exported function. Keep both files in the same folder and run node app.js.

const addFunction = require('./mathUtils.js');

console.log("Using the imported function:");
console.log(`5 + 3 = ${addFunction(5, 3)}`);
console.log(`10 + 2 = ${addFunction(10, 2)}`);

CommonJS: Exporting Multiple Items

Need to export several things? Assign an object to module.exports holding all of them — a clean way to group related utilities.

const greet = (name) => `Hello, ${name}!`;
const farewell = (name) => `Goodbye, ${name}!`;

module.exports = {
  greetUser: greet,
  sayGoodbye: farewell
};

CommonJS: Importing Multiple Items

When a module exports an object, grab pieces with destructuring: const { greetUser, sayGoodbye } = require(...). Cleaner and clearer than dot access.

const { greetUser, sayGoodbye } = require('./utils.js');

console.log(greetUser('Alice'));
console.log(sayGoodbye('Bob'));

ES Modules: The Modern Standard

ES Modules (ESM) are JavaScript's official standard, using import/export. They enable tree-shaking — set "type": "module" or use .mjs to opt in.

ESM: Named Exports & Imports

With named exports, you export values by name and import them in matching { } braces — making it obvious exactly which parts you're using.

export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;

// app.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node app.mjs`
import { add, multiply } from './calculator.mjs';

console.log(`2 + 4 = ${add(2, 4)}`);
console.log(`5 * 6 = ${multiply(5, 6)}`);

ESM: Default Exports

A module can have one default export, usually its main feature. Import it under any name you like, no braces needed.

const logMessage = (msg) => console.log(`[LOG] ${msg}`);
export default logMessage;

// main.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node main.mjs`
import myLogger from './logger.mjs';

myLogger('This is a default message.');
myLogger('Another important event.');

Quick Check: Module Systems

You've learned about CommonJS and ES Modules. Let's test your understanding!

Recap: Node.js Modules

You've explored Node modules: CommonJS (require/module.exports, the classic) and ESM (import/export, the modern standard). Both keep apps organized.

Frequently asked questions

Is the “Node.js Module System Explained” lesson free?

Yes — the full text of “Node.js Module System Explained” is free to read here on the web, and the Node.js Backend Development Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Node.js Backend Development Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Node.js Module System Explained”?

Explore CommonJS modules, ES modules, and how to import and export functionality across files in Node.js. You practise Node.js Backend Development Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Node.js Backend Development Bootcamp?

No prior experience is required. Node.js Backend Development Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Node.js Module System Explained” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Node.js Backend Development Bootcamp lesson?

Yes. Every Node.js Backend Development Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Node.js & NPM
  2. Node.js Module System Explained
  3. Asynchronous JavaScript & Event Loop
  4. Working with the File System & Streams
← Back to Node.js Backend Development Bootcamp