Node.js lesson · 30 min

Node.js CommonJS Modules

Split a Node.js project into real files using `require` and `module.exports`.

Code runs locally in your browser

What you will practice

  • Understand why modules exist in Node.js.
  • Recognize CommonJS terms.
  • Practice separating reusable functions.

What this means

Node.js has historically used CommonJS modules with `require` and `module.exports`.

`module.exports` chooses what a file shares. `require` loads what another file exported.

Modern Node.js can also use ES modules with `import` and `export`, but many real projects still contain CommonJS.

If this is your first time seeing this

CommonJS is one way Node.js files share code.

Think of `module.exports` as the public shelf and `require` as taking something from that shelf.

Mini glossary

require
CommonJS syntax for loading another module.
module.exports
The value a CommonJS file shares with other files.
Module boundary
The line between private code inside a file and public code exported from it.

Example from everyday life

CommonJS is like lending a labeled tool from one drawer to another. One drawer says what it offers, and another drawer asks for that exact tool by name.

How it works step by step

  1. Create `math.js` and export functions with `module.exports`.
  2. Create `index.js` and load the exported functions with `require`.
  3. Run `node index.js` from the same folder.
  4. Change one function in `math.js` and rerun the app to see how modules keep logic reusable.

Where you will use this

  • A Node.js app exports database helpers from one file.
  • A test file requires a function and checks its behavior.
  • A CLI project splits parsing, validation, and output into modules.

Before you run the code

The syntax is different from ES modules, but the idea is the same: share focused code across files.

As a beginner, care more about the boundary than the exact module flavor. Later you can compare CommonJS with ES modules.

Run it locally

These examples are meant for a local Node.js project. Create the files shown below, then run the terminal commands from the project folder.

Create the files

mkdir lumio-commonjs-demo
cd lumio-commonjs-demo
touch math.js index.js

Export reusable functions

math.js
function add(a, b) {
  return a + b;
}

function average(numbers) {
  const total = numbers.reduce((sum, value) => sum + value, 0);
  return total / numbers.length;
}

module.exports = {
  add,
  average
};

Require the module

index.js
const { add, average } = require("./math");

const scores = [8, 10, 9];

console.log(`Total: ${add(4, 6)}`);
console.log(`Average score: ${average(scores)}`);

Run it

node index.js

Common beginner mistakes

  • Exporting too much and making every file depend on every other file.
  • Confusing CommonJS with ES modules.
  • Copying functions between files instead of exporting reusable logic.

Why there is no browser runner here

This lesson uses real Node.js APIs and terminal commands. Run the examples locally with Node.js so you learn the same workflow used in actual backend projects.

Try changing this next

  • Add a `triple` function to the exported object.
  • Rename `mathFile` to `calculatorFile`.
  • Print the names of all exported functions.