JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Modules

javascript
senior
modules

Explain the Module pattern and how ES6 modules differ from CommonJS.

modules
es6-modules
commonjs
patterns
imports
exports
Quick Answer

The Module pattern encapsulates code using closures to create private state. ES6 modules are the standard with static imports (analyzed at compile time), while CommonJS uses dynamic require() at runtime. ES6 supports tree-shaking and async loading.

Detailed Explanation

Classic Module Pattern:

  • Uses IIFE and closures for encapsulation
  • Creates private variables and public API
  • Predates native module systems

CommonJS (Node.js):

  • require() for imports, module.exports for exports
  • Synchronous loading (blocking)
  • Dynamic: imports can be conditional
  • Copies of exported values

ES6 Modules:

  • import/export syntax
  • Static: analyzed at compile time
  • Asynchronous loading
  • Live bindings (not copies)
  • Enables tree-shaking
  • Strict mode by default

Code Examples

Classic Module Pattern (IIFE)JavaScript
// Revealing Module Pattern
const Calculator = (function() {
  // Private state
  let result = 0;
  
  // Private function
  function validate(n) {
    if (typeof n !== 'number') throw new Error('Not a number');
  }
  
  // Public API
  return {
    add(n) {
      validate(n);
      result += n;
      return this;
    },
    subtract(n) {
      validate(n);
      result -= n;
      return this;
    },
    getResult() {
      return result;
    },
    reset() {
      result = 0;
      return this;
    }
  };
})();

Calculator.add(5).add(3).subtract(2).getResult(); // 6
// Calculator.result is undefined (private)

Real-World Applications

Use Cases

Dual-Format NPM Package Publishing

Publishing libraries with both ESM and CJS builds using package.json conditional exports for maximum ecosystem compatibility

Micro-Frontend Module Sharing

Using Module Federation or import maps to share modules across independently deployed micro-frontends at runtime

Server-Side Module Isolation

Using the IIFE Module pattern for isolated service instances in serverless environments where each invocation needs clean state

Mini Projects

Module System Polyfill

intermediate

Implement a simplified CommonJS-style require() in the browser using IIFE and closures with module caching and circular dependency detection

Live Binding Demo Tool

advanced

Build a visual tool showing ESM live bindings vs CJS value copies side-by-side as values mutate in real-time

Industry Examples

Deno

Exclusively uses ES Modules with built-in CJS-to-ESM conversion tools for Node.js compatibility

Webpack

Module Federation enables runtime module sharing across independently deployed applications

Resources

MDN - JavaScript modules

docs

Node.js - Modules: CommonJS modules

docs
Previous
How does JavaScript garbage collection work and how can you prevent memory leaks?
Next
What is CORS, when do CORS errors occur, and how can they be resolved?
PrevNext