Modules (CommonJS & ESM)

A module in Node.js is a self-contained file of code whose values you can share with other files — using CommonJS (require / module.exports) or modern ES Modules (import / export).

Learn Modules (CommonJS & ESM) in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll be able to split a program across several files, share functions between them with both module systems, pull in Node's built-in core modules, and recognise the small differences between named and default exports.

What You'll Learn in This Lesson

1️⃣ CommonJS: module.exports and require()

CommonJS is Node's original module system, and you'll still see it in the vast majority of existing projects. The idea is simple: one file attaches values to module.exports , and another file reads them back with require() . Here's a math.js file that exports two functions.

Now a second file, app.js , brings those functions in with require('./math') . The leading ./ is important: it tells Node "look for a local file", not an installed package.

2️⃣ ES Modules: export and import

ES Modules (ESM) is the official JavaScript standard, the same one browsers use. Instead of module.exports you write export , and instead of require() you write import . To switch a Node project to ESM you either add {' '} to your package.json or give files the .mjs extension.

Notice the two flavours of export above: named exports ( add , multiply ) and a single default export ( subtract ). When you import, the default comes with no braces and the named ones go inside {' '} . In ESM you must include the file extension on local imports.

Your turn. The two files below work once you fill in the two blanks marked ___ . Follow the 👉 hints, then run it and compare with the expected output.

3️⃣ Built-in Core Modules and node: Imports

Node ships with dozens of core modules — ready-made tools like path (file paths), os (operating-system info), fs (files), and http (web servers). You don't install them; they come with Node. The modern style writes the node: prefix (for example require('node:path') ) so it's obvious you mean the built-in, not an npm package.

Whichever module system you use, it helps to keep the difference between named and default exports straight. Here's a side-by-side comparison.

No blanks this time — just a brief and an outline to keep you on track. Write the two files yourself, run them, and check your output against the example in the comments. Splitting one job across two files is exactly the habit that scales to real projects.

📋 Quick Reference — Modules

Practice quiz

In CommonJS, how do you export values from a file?

  • export default x
  • module.exports = x
  • exports x
  • share x

Answer: module.exports = x. CommonJS assigns what you want to share to module.exports.

Which function imports a module in CommonJS?

  • import()
  • include()
  • require()
  • load()

Answer: require(). require('./file') brings in a local CommonJS module.

What does the './' prefix in require('./math') mean?

  • A package from npm
  • A core built-in module
  • A global variable
  • A local file in the same folder

Answer: A local file in the same folder. './' tells Node to look for a local file, not an npm package.

Which keyword declares a NAMED export in ES modules?

  • export
  • module
  • share
  • public

Answer: export. ESM uses the export keyword for named exports.

How many default exports can a single ES module have?

  • Unlimited
  • Exactly one
  • Zero only
  • Two

Answer: Exactly one. A file can have many named exports but only one default export.

When importing a default export, named imports go in braces. How is the default written?

  • In square brackets
  • Without braces, with any name you choose
  • Always named 'default'
  • In double quotes

Answer: Without braces, with any name you choose. The default export is imported without braces and you may name it anything.

Which prefix makes it explicit you mean a built-in core module?

  • core:
  • lib:
  • std:
  • node:

Answer: node:. The node: prefix (e.g. require('node:path')) marks a built-in module.

What does package.json need so .js files are treated as ES modules?

  • "type": "module"
  • "esm": true
  • "module": "es"
  • "format": "esm"

Answer: "type": "module". Setting "type": "module" enables ESM for .js files.

In ESM, what is required on local file imports that CommonJS lets you omit?

  • The word import
  • Quotes
  • The file extension (.js / .mjs)
  • A leading slash

Answer: The file extension (.js / .mjs). ESM requires the explicit file extension on local imports.

What does require('./math') return when math.js does module.exports = { add, multiply }?

  • Only add
  • undefined
  • A Promise
  • The object { add, multiply }

Answer: The object { add, multiply }. require returns exactly whatever was assigned to module.exports.