1. CommonJS import
require() synchronously loads a module and returns its exports — the CommonJS way.
const fs = require("fs");Typorithm · easy
Step 1 of 8 · intro card
Modules & npm
CommonJS uses require() and module.exports; ES Modules use import and export. Node treats .js as CommonJS unless package.json sets "type": "module" (or you use the .mjs extension).
Node ships two module systems. CommonJS (CJS) loads dependencies with require() and exposes values via module.exports; it has been Node's default since the start.
ES Modules (ESM) are the language standard: import pulls in bindings and export (default or named) exposes them. Imports are static and resolved before the file executes.
Node decides which system a .js file is by package.json: with "type": "module" it parses files as ESM, otherwise as CommonJS. The .mjs and .cjs extensions force one mode regardless.
CommonJS: require() / module.exports.
ESM: import / export default / export const.
"type": "module" makes .js files ESM.
// CommonJS
const fs = require("fs");
module.exports = foo;
// ES Modules
import fs from "fs";
export default foo;
export const x = 1;Connect each part of the code to the concept before you type.
require() synchronously loads a module and returns its exports — the CommonJS way.
const fs = require("fs");module.exports assigns what require() will return from this file.
module.exports = foo;import is the static, standard ES Modules syntax, resolved before the module runs.
import fs from "fs";export default declares the single main value a module exposes.
export default foo;export const declares a named binding importers pull in by name.
export const x = 1;Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.