DEV Community

Cover image for ES Modules and CommonJS: An Overview
Matheus Costa
Matheus Costa

Posted on

ES Modules and CommonJS: An Overview

JavaScript has come a long way since its inception and has grown into one of the most widely used programming languages in the world. With the introduction of ECMAScript 6 (ES6) in 2015, a standardized module system was introduced in the form of ES modules.

CommonJS

CommonJS is an older module system for JavaScript that was designed for server-side JavaScript development with Node.js. It was later adopted by the front-end community as well (this way you don't need a gazillion script tags in your HTML). CommonJS modules work by using require statements to load dependencies, and module.exports statements to define the exports of a module.

Here's an example of using CommonJS modules:

// my-module.js
module.exports = {
  myValue: 42
};

// main.js
const myModule = require('./my-module.js');
console.log(myModule.myValue); // 42
Enter fullscreen mode Exit fullscreen mode

ES Modules

ES modules are a standardized module system for JavaScript that was introduced in ES6. It provides a way to organize and reuse code in a modular and maintainable manner. ES modules allow developers to define exports (the values or functions they want to make available to other parts of their code), and import them into other parts of their code.

Here's an example of using ES modules:

// my-module.js
export const myValue = 42;

// main.js
import { myValue } from './my-module.js';
console.log(myValue); // 42
Enter fullscreen mode Exit fullscreen mode

One of the main benefits of ES modules is that their import and export statements are designed to be statically analyzable, whereas CommonJS modules can be analyzed statically but require additional tooling to do so. This means that the import and export statements can be analyzed at build time, enabling tools like bundlers and compilers to optimize the code and generate smaller, more efficient bundles for production use.

ES modules also provide a way to specify the dependencies between different parts of your code, making it easier to understand and maintain your codebase. And since ES modules are natively supported in modern browsers and Node.js, there's no need for additional build tools or plugins to use them.

Advantages of ES Modules over CommonJS

  1. Static Analysis: ES modules are designed to be statically analyzable, while CommonJS modules are not. This means that the import and export statements can be analyzed at build time, enabling tools to optimize the code and generate smaller, more efficient bundles.

  2. Native Support in Browsers: ES modules are natively supported in modern browsers, while CommonJS modules are not. This means that there's no need for additional build tools or plugins to use ES modules in the browser.

  3. Better Performance: ES modules are designed to be loaded statically, while CommonJS modules are mainly loaded dynamically and synchronously. This can lead to slower performance and a blocking of the main thread. Static analysis refers to the process of analyzing code without executing it, and dynamic imports introduce runtime behavior into a module system. This means that the exact module that will be imported cannot be determined until the code is executed, making it difficult to analyze the module dependencies and relationships ahead of time (AOT).

  4. Improved Code Organization: ES modules provide a way to specify the dependencies between different parts of your code, making it easier to understand and maintain your codebase.

In conclusion, ES modules provide a powerful and flexible way to organize and reuse code in JavaScript, making it easier to build and maintain complex applications. With its advantages over CommonJS, ES modules are now the recommended way to organize and reuse code in JavaScript, and are widely adopted in the JavaScript community.

Top comments (5)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
costamatheus97 profile image
Matheus Costa

Thanks! I'm glad you liked it

Collapse
 
trueromanus profile image
Roman Vladimirov

Why compare CommonJS, which is often used on the backend (in Node.js), and ES modules, which were originally created to unify the frontend and backend? Yes, CommonJS can be used in the frontend, but in many cases it's just because some libraries are already written in CommonJS for the backend and after a while they also need to use it in the frontend. There is also an AMD library for the frontend that solves some CommonJS problems (like synchronized launch), and there is even a UMD loader that combines CommonJS and AMD in one module. UMD solves issue if the same code needs to be run on the frontend and backend. ES modules replace libraries in the UMD module.

Collapse
 
costamatheus97 profile image
Matheus Costa

Why shouldn't I give an overview on the two used module systems?

CommonJS was built when there was no module system at all, ES Modules came to replace it. It doesn't need a module loader. CommonJS modules are imported dynamically and at runtime by default, which does not allow static analysis. New projects should use ES Modules as it provides better tooling and ecossystem compatibility, and its asynchronous nature can lead to improvements in performance.

Collapse
 
trueromanus profile image
Roman Vladimirov

My comment above was about comparison context. If you compare CommonJs and ES modules on the backend, it's a completely relevant comparison, because CommonJs was single options for backend before ES Modules.