Full answer
When Node loads a file with require, it resolves the full path, checks require.cache (keyed by that path), and returns the cached exports if present. If not, it creates a module object, adds it to the cache before running the code, runs the file, and returns module.exports.
Caching has two effects:
- A module is a singleton per resolved path. A file with top-level state (a counter, a database pool) shares that state with every file that requires it.
- Top-level code runs once. Requiring the same file twice prints its console.log once.
Because the module is added to the cache before it finishes running, circular dependencies do not loop forever. If a.js requires b.js, and b.js requires a.js at the top, b.js gets the exports object of a.js as it is at that moment. If a.js has not yet assigned its exports, b.js sees an empty object. Functions added to exports later become visible on the same object, but replacing the object with module.exports = {...} later does not reach b.js.
Fixes: avoid the cycle by moving shared code to a third module, require lazily inside the function that needs it, or add to exports (exports.x = ...) before requiring the other file.
Deleting a key from require.cache forces a reload. That is used by some test tools and is not safe for application code.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cjs-'));
const write = (name, text) => fs.writeFileSync(path.join(dir, name), text);
write('a.js', `
console.log('a starts');
exports.early = 'set before requiring b';
const b = require('./b.js');
console.log('a sees from b:', b.fromA);
exports.late = 'set after requiring b';
console.log('a ends');
`);
write('b.js', `
console.log('b starts');
const a = require('./a.js'); // a is only partly loaded here
console.log('b sees from a:', Object.keys(a));
exports.fromA = a.early;
console.log('b ends');
`);
require(path.join(dir, 'a.js'));
require(path.join(dir, 'a.js')); // second call: nothing runs, cache is used
console.log('files in cache:', Object.keys(require.cache).filter((f) => f.startsWith(dir)).length);
fs.rmSync(dir, { recursive: true });b.js sees only the early property of a.js, because a.js is still in the middle of loading. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): a starts b starts b sees from a: [ 'early' ] b ends a sees from b: set before requiring b a ends files in cache: 2
Key points
- Modules are cached by resolved path and run once.
- The cache gives singleton behaviour.
- A cycle gives the second module a partial exports object.
- Best fix: remove the cycle by extracting shared code.
Common mistakes
- Thinking require runs the file again each time.
- Replacing module.exports after another file has already required the module in a cycle.
- Hiding a cycle with try/catch.
- Keeping mutable global state in a module and expecting a fresh copy per require.
Follow-ups
- How would you reset a module between two tests?
- Does ESM handle cycles differently?
- Where does the cache key come from when the same package is installed twice?
Tips
- Walk through the sequence of logs in the snippet out loud.
- Explain that the module is cached before it finishes running.
- Offer two fixes, and prefer restructuring.