0Pricing
JavaScript Academy · Lesson

CommonJS basics — require/module.exports and exports

Learn CommonJS: require(), module.exports, exports shortcut, and a small interop taste with ESM.

CommonJS basics — require/module.exports and exports is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. 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 JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

CJS: big picture

Goal: Read and write small CommonJS modules.

  • require() loads modules
  • module.exports is the export object
  • exports is a shortcut to the same object
  • Quick ESM interop taste
CommonJS basics — require/module.exports and exports — illustration 1

require() — idea

require(path) returns the module's exported object in CommonJS.

// CommonJS import uses require()
const cjsExample = [
  "const math = require(\\"./math.js\\");",
  "console.log(math.sum(2, 3));"
];

console.log("require() example:");
for (const line of cjsExample) console.log(line);

// Emulate a loaded module object
const loaded = { sum: function (a, b) { return a + b; } };
console.log("emulated require sum:", loaded.sum(2, 3));
CommonJS basics — require/module.exports and exports — illustration 2

module.exports

Assign a value to module.exports to export it (function, object, etc.).

// module.exports sets what the module returns
const lines1 = [
  "function greet(name) { return \\"Hi \\" + name; }",
  "module.exports = greet;"
];

console.log("module.exports example:");
for (const l of lines1) console.log(l);

// Emulate consumption
function greet(name) { return "Hi " + name; }
const api1 = greet; // like module.exports = greet
console.log("use exported fn:", api1("Ayla"));
CommonJS basics — require/module.exports and exports — illustration 3

exports shortcut

exports.prop = ... is fine; do not reassign exports = ... because it no longer points to module.exports.

// exports is a shortcut to module.exports (same object)
const lines2 = [
  "// Good: add properties",
  "exports.sum = function (a, b) { return a + b; };",
  "",
  "// Pitfall: reassigning exports breaks the link",
  "exports = function () {}; // WRONG"
];

console.log("exports shortcut:");
for (const l of lines2) console.log(l);

// Emulate good usage
const moduleObj = {};
const exportsRef = moduleObj;  // like exports === module.exports initially
exportsRef.sum = function (a, b) { return a + b; };
console.log("sum via exportsRef:", moduleObj.sum(2, 4));
CommonJS basics — require/module.exports and exports — illustration 4

Multiple exports

You can export an object holding many functions and read them via properties.

// Export an object with multiple members
const lines3 = [
  "function add(a, b) { return a + b; }",
  "function mul(a, b) { return a * b; }",
  "module.exports = { add, mul };"
];

console.log("multiple exports object:");
for (const l of lines3) console.log(l);

// Emulate consumption
const api2 = {
  add: function (a, b) { return a + b; },
  mul: function (a, b) { return a * b; }
};
console.log("use add/mul:", api2.add(1, 2), api2.mul(2, 3));
CommonJS basics — require/module.exports and exports — illustration 5

ESM ↔ CJS taste

Interop (taste):

  • ESM importing CJS often receives the CJS module.exports as the default.
  • Example: import pkg from "./cjs.js" then use pkg.add().
  • Keep examples simple; avoid mixing styles in one file unless you must.
CommonJS basics — require/module.exports and exports — illustration 6

CJS export syntax quiz

Quick check: Export in CommonJS.

CommonJS basics — require/module.exports and exports — illustration 7

Recap

Recap: Load with require(), export with module.exports, add properties via exports.prop, and avoid reassigning exports. ESM interop often maps CJS module.exports to a default.

CommonJS basics — require/module.exports and exports — illustration 8

Frequently asked questions

Is the “CommonJS basics — require/module.exports and exports” lesson free?

Yes — the full text of “CommonJS basics — require/module.exports and exports” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “CommonJS basics — require/module.exports and exports”?

Learn CommonJS: require(), module.exports, exports shortcut, and a small interop taste with ESM. You practise JavaScript Academy 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 JavaScript Academy?

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

How long does the “CommonJS basics — require/module.exports and exports” 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 JavaScript Academy lesson?

Yes. Every JavaScript Academy 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. ESM basics — import/export, default vs named
  2. CommonJS basics — require/module.exports and exports
  3. Module resolution in Node and browser bundlers
← Back to JavaScript Academy