Modules in JavaScript – From Classic Patterns to ES Modules and Structural Differences

Modules in JavaScript, like classes, are designed to group data and behavior into logical units. This article explores the classic module pattern using factory functions, compares it to class-based design, and introduces ES Modules (ESM) introduced in ES6. It explains how modules are defined, exported, imported, and instantiated, and highlights the differences in structure and usage.

moduleESMfactory-functionimport-export

~3 min read · Updated Oct 20, 2025

Introduction


Modules in JavaScript serve the same purpose as classes: grouping related data and behavior. They can interact with other modules and share functionality. However, they differ significantly in syntax and structure — especially between classic modules and ES Modules (ESM).


Classic Modules


Before ES6, modules were commonly implemented using functions that returned a public API object. These are known as factory functions:


function Publication(title, author, pubDate) {
  var publicAPI = {
    print() {
      console.log(`
Title: ${title}
By: ${author}
${pubDate}
`);
    }
  };
  return publicAPI;
}

Classic modules encapsulate data within the function scope and expose only selected methods. Unlike classes, they don’t use this, and private data is naturally protected.


Comparison with Classes


Classes store data and methods on object instances, and everything is public by default. In contrast, classic modules return only what should be public, keeping the rest private inside the factory function.


Instantiating Classic Modules


To use a classic module, simply call the function — no new keyword required:


var YDKJS = Book({
  title: "You Don't Know JS",
  author: "Kyle Simpson",
  publishedOn: "June 2014",
  publisher: "O'Reilly",
  ISBN: "123456-789"
});
YDKJS.print();

ES Modules (ESM)


ES6 introduced a formal module syntax. Key features of ESM:


  • Each file is a module.
  • export defines the public API.
  • import brings in functionality from other modules.
  • Modules are singletons — only one instance is created and shared.

If multiple instances are needed, use a factory function inside the ESM.


Example: Mixing ESM and Classic Modules


// publication.js
function printDetails(title, author, pubDate) {
  console.log(`
Title: ${title}
By: ${author}
${pubDate}
`);
}
export function create(title, author, pubDate) {
  var publicAPI = {
    print() {
      printDetails(title, author, pubDate);
    }
  };
  return publicAPI;
}

In another file:


// blogpost.js
import { create as createPub } from "publication.js";
export function create(title, author, pubDate, URL) {
  var pub = createPub(title, author, pubDate);
  var publicAPI = {
    print() {
      pub.print();
      console.log(URL);
    }
  };
  return publicAPI;
}

Conclusion


Modules in JavaScript are powerful tools for organizing code. Classic modules offer flexibility and natural encapsulation, while ES Modules provide a formal, file-based structure. Understanding the differences and choosing the right pattern for your needs is essential for writing clean and scalable JS applications.


Written & researched by Dr. Shahin Siami

Related Articles

Coercive Conditional Comparison and Prototypal Classes in JavaScript – Understanding Implicit Logic and Legacy Inheritance Patterns

This article explores two advanced topics from Appendix A of You Don’t Know JS Yet: how JavaScript performs coercive comparisons in conditional expressions, and how prototypal class patterns were used before ES6 introduced the class keyword. Through practical examples, it clarifies how implicit boolean logic works and how prototype chains enable behavior delegation.

Continue

Value vs Reference and the Many Forms of Function Definitions

Value vs Reference and the Many Forms of Function Definitions

Continue

Prototypes in JavaScript – Delegation, Prototype Chains, and Dynamic this Behavior

Prototypes in JavaScript provide a mechanism for property delegation between objects. This article explains how prototype chains work, how to create linked objects using Object.create, and how property access and assignment behave in relation to delegation. It also explores how the dynamic nature of this enables prototype-based method reuse across multiple objects.

Continue

Closure and this in JavaScript – Scope Memory and Dynamic Execution Context

Closure and this are two foundational and often misunderstood concepts in JavaScript. This article defines closure as a function’s ability to remember variables from its outer scope, and explains how this refers to the dynamic execution context of a function call. Through practical examples, it clarifies the difference between static scope and dynamic context.

Continue

Iteration in JavaScript – The Iterator Pattern, Built-in Iterables, and Standard Data Consumption

Iteration is a foundational pattern for processing data in JavaScript. This article explores the iterator protocol, how to consume iterators using for..of loops and the spread operator, the distinction between iterators and iterables, and how built-in structures like arrays, strings, maps, and sets support standardized iteration. It also shows how to customize iteration for your own data structures.

Continue

Organizing Code in JavaScript – Classes, Modules, and Object-Oriented Inheritance

JavaScript offers two major patterns for organizing code: classes and modules. This article focuses on class-based design, including how to define and instantiate classes, encapsulate data and behavior, and use inheritance and polymorphism to extend functionality. Understanding these patterns helps developers write clean, maintainable, and scalable programs.

Continue