What is the require Function in Node.js?
Learn how Node.js require() loads and caches CommonJS modules, its full module resolution rules, and how it compares to ES module import syntax.
Expected Interview Answer
require() is Node's CommonJS function for importing modules — it synchronously loads a file or package, executes it once, and returns whatever that module assigned to module.exports, caching the result so subsequent requires of the same module return the same cached instance.
When you call require('./util'), Node resolves the path (adding .js/.json/.node extensions or looking for index.js), wraps the file in a function scope providing module, exports, require, __dirname, and __filename, then executes it. The returned value is whatever was set on module.exports (or the shorthand exports object). Because Node caches modules by resolved filename, requiring the same file twice returns the identical object reference rather than re-executing it — useful for singletons but a common source of bugs if you expect fresh state. With the rise of ES modules (import/export, enabled via 'type': 'module' or .mjs), require is now considered the CommonJS-era mechanism, though it remains dominant in existing codebases and is still synchronous, unlike dynamic import().
- Synchronous, simple module loading
- Automatic caching avoids re-executing modules
- Works with core modules, npm packages, and local files
- Provides module.exports for flexible export shapes
- Well-understood, battle-tested across the Node ecosystem
AI Mentor Explanation
require() is like calling up a specific player's stats sheet from the league's central archive — the first time you request it, the archive compiles and files the sheet, but every subsequent request for that same player instantly returns the identical filed copy rather than recompiling it. Ask for a different player's file and it goes through the same one-time compilation before being cached.
Step-by-Step Explanation
Step 1
Call require with a path or module name
e.g. require('./util') for local files or require('express') for packages.
Step 2
Node resolves the module path
It tries exact match, then .js/.json/.node extensions, then index.js in a directory.
Step 3
Node wraps and executes the file
The file runs inside a function wrapper exposing module, exports, require, __dirname, __filename.
Step 4
The module sets module.exports
Whatever is assigned to module.exports becomes the returned value.
Step 5
Node caches the result
Subsequent require() calls for the same resolved path return the cached exports object without re-executing.
What Interviewer Expects
- Explains require() is synchronous and CommonJS-based
- Knows module resolution order (extensions, index.js)
- Understands module caching by resolved filename
- Can contrast require() with ES module import
- Mentions module.exports vs exports shorthand
Common Mistakes
- Reassigning 'exports' directly instead of module.exports and losing the reference
- Assuming require() re-executes a module on every call
- Mixing require() and import in the same file incorrectly
- Not understanding circular require dependencies can return partial exports
Best Answer (HR Friendly)
“require() is how older-style Node.js code loads other files or libraries into your program. The first time you require a file, Node runs it and remembers the result, so asking for the same file again is instant and doesn't rerun the code.”
Code Example
// math.js
function add(a, b) { return a + b; }
module.exports = { add };
// app.js
const { add } = require('./math');
console.log(add(2, 3)); // 5
const mathAgain = require('./math');
console.log(mathAgain === require('./math')); // true (cached)Follow-up Questions
- How does Node resolve a module path passed to require()?
- Why does require() return the same object on repeated calls?
- What's the difference between module.exports and exports?
- How do circular requires behave in Node?
- How does require() differ from ES module import syntax?
MCQ Practice
1. What does require() return?
require() returns the value assigned to module.exports in the loaded file.
2. What happens when you require the same module twice?
Node caches modules by resolved path, so repeated requires return the same cached exports object.
3. Is require() synchronous or asynchronous?
require() loads and executes modules synchronously, blocking until the module is fully loaded.
Flash Cards
What does require() do? — Synchronously loads a module and returns its module.exports value.
Is require() cached? — Yes — Node caches modules by resolved filename, so repeat requires return the same object.
What's exposed inside a required file? — module, exports, require, __dirname, and __filename via a function wrapper.
How does require() differ from import? — require() is synchronous CommonJS; import is the ES module syntax, supporting static analysis and async loading.