1. Concept library
  2. Backend with Node.js
  3. Module 1: The Node.js runtime

Concept

Modules in Node

Node supports CommonJS (require) and ES modules (import). New projects should use ES modules with "type": "module" in package.json, and know how to load the occasional CommonJS package.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain the difference between CommonJS and ES modules
  2. Set up a project with "type": "module" and import between files
  3. Spot the missing file extension behind "Cannot find module"

In practice

A few lines that show Modules in Node doing its job.

JavaScript
// package.json: { "type": "module" }

// money.js
export function format(cents) { return "$" + (cents / 100).toFixed(2); }

// server.js
import { format } from "./money.js";       // the extension is required
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const legacy = require("some-commonjs-package");

The common mistake

Watch for

Leaving the file extension off a relative import. ES modules in Node require "./money.js", and the error, "Cannot find module", does not say so.

How it stays learned

Once a session shows you understand Modules in Node, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Modules in Node.

Previous in the module← The event loop
Next in the modulenpm and package.json →