Node.js Modules and require Interview Questions CommonJS vs ES Modules
Master Node.js module system interview questions require vs import, module.exports vs export, caching, and circular dependencies.
Node.js Modules and require Interview Questions
The module system is a core Node.js concept. Here are the most common interview questions.
Q1: What Is the Difference Between require and import?
Answer:
- require (CommonJS) Synchronous, used in Node.js traditionally
- import (ES Modules) Asynchronous, standard JavaScript, supported in Node.js 13+
// CommonJS const express = require("express"); module.exports = { myFunction }; // ES Modules import express from "express"; export { myFunction }; // or export default { myFunction };
Q2: How Does require() Work?
Answer: require() follows these steps:
- Resolution Find the file (core module, node_modules, or file path)
- Loading Read the file content
- Wrapping Wrap in a function:
(function(exports, require, module, __filename, __dirname) { ... }) - Evaluation Execute the wrapped function
- Caching Cache the module for future requires
Q3: What Is Module Caching?
Answer: Node.js caches every required module. The second require returns the cached module:
// counter.js let count = 0; module.exports = { increment: () => count++, getCount: () => count }; // app.js const counter1 = require("./counter"); const counter2 = require("./counter"); // Same object as counter1 counter1.increment(); console.log(counter2.getCount()); // 1 same instance
To clear cache: delete require.cache[require.resolve("./counter")]
Q4: What Are Circular Dependencies and How Does Node.js Handle Them?
Answer: Circular dependencies occur when module A requires module B, and B requires A.
// a.js const b = require("./b"); console.log("a.js loaded, b =", b); // b.js const a = require("./a"); console.log("b.js loaded, a =", a);
Node.js handles this by returning a partial (incomplete) module.exports object. When b.js requires a.js, it gets the current state of a's exports (which may be incomplete).
Best practice: Avoid circular dependencies by refactoring shared logic into a third module.
Q5: What Is the Difference Between module.exports and exports?
Answer:
// These are the same initially: exports === module.exports // true // WRONG reassigns exports, breaks the reference exports = { myFunc: () => {} }; // RIGHT adds to module.exports exports.myFunc = () => {}; // or module.exports = { myFunc: () => {} };
Always use module.exports = ... to be safe.
Q6: What Are Core Modules in Node.js?
Answer: Core modules are built into Node.js:
http/httpsHTTP server/clientfsFile systempathPath utilitiesosOS informationcryptoCryptographyeventsEvent emitterstreamStreamsutilUtilitieschild_processSpawn processesurlURL parsing
const fs = require("fs"); // Core module const express = require("express"); // node_modules const myModule = require("./myModule"); // Local file
The Takeaway
Module system interview questions cover: require vs import (CommonJS vs ES Modules), how require works (resolve, load, wrap, evaluate, cache), module caching (same instance on repeated requires), circular dependencies (partial exports avoid by refactoring), module.exports vs exports (always use module.exports), and core modules (http, fs, path, crypto, events, stream).
require (CommonJS) is synchronous and traditionally used in Node.js. import (ES Modules) is asynchronous, is the JavaScript standard, and is supported in Node.js 13+ with type: 'module' in package.json. require uses module.exports, import uses export/export default.
Five steps: resolution (find the file), loading (read content), wrapping (wrap in a function with exports, require, module, __filename, __dirname), evaluation (execute the function), and caching (store for future requires). Cached modules return the same instance on subsequent requires.
When module A requires B and B requires A, Node.js returns a partial (incomplete) module.exports object. When B requires A, it gets the current state of A's exports, which may be incomplete. Best practice: avoid circular dependencies by refactoring shared logic into a third module.
Initially, exports and module.exports point to the same object. If you reassign exports (exports = { ... }), it breaks the reference and module.exports is not updated. Always use module.exports = { ... } or exports.property = value (adding properties, not reassigning).
Built-in modules: http/https (web server), fs (file system), path (path utilities), os (OS info), crypto (cryptography), events (event emitter), stream (streams), util (utilities), child_process (spawn processes), url (URL parsing). They don't need installation require('fs') works directly.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

