Module Mania: Understanding the difference between CJS, AMD, UMD and ESM
In this article we're going to talk about Module Systems. Module systems are important in modern web development because they help us organize our code and avoid naming conflicts. There are four common module systems: CommonJS (CJS), Asynchronous Module Definition (AMD), Universal Module Definition (UMD), and ECMAScript Modules (ESM). In this article, we'll discuss each of these module systems and their differences.
Date Posted : 19 September 23
Time to read: 5 mins
“A good programmer is someone who always looks both ways before crossing a one-way street.”Doug Linder
What the hell are modules systems anyway?
In simple terms, modules are just reusable pieces of code. They allow you to break your code into smaller, more manageable pieces, which makes it easier to test and maintain. Think ‘Modular kitchen’ , ‘Modularizing’ etc.
Module systems provide a way to load and use these modules in your code. They define a standardized way of importing and exporting code between different files, which can be especially helpful in larger projects.
There are several different module systems available, including CommonJS, AMD, UMD, and ESM. Each system has its own syntax and features, but they all serve the same basic purpose: to help you organize your code and make it more modular and reusable.
Common Module Systems: CJS, AMD, UMD, and ESM
There are four common module systems in Javascript : CommonJS (CJS), Asynchronous Module Definition (AMD), Universal Module Definition (UMD), and ECMAScript Modules (ESM). In this article, we’ll discuss each of these module systems and their differences.
CommonJS (CJS)
CommonJS is a module system used by Node.js. It uses the require()
function to load modules synchronously. CJS modules export an object, and any exported value becomes part of that object. For example, the following code exports and imports a function:
// example.js
function hello() {
console.log('Hello, world!');
}
module.exports = hello;
// app.js
const hello = require('./example');
hello();
Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD) is a module system designed for the browser. It uses the define()
function to define modules, and it loads modules asynchronously. AMD modules export an object, and any exported value becomes part of that object. For example, the following code exports and imports a function:
// example.js
define(function() {
function hello() {
console.log('Hello, world!');
}
return hello;
});
// app.js
require(['./example'], function(hello) {
hello();
});
Universal Module Definition (UMD)
Universal Module Definition (UMD) is a hybrid module system that supports both synchronous and asynchronous loading. It uses a combination of CommonJS and AMD syntax to provide a flexible and modular system. UMD modules export an object, and any exported value becomes part of that object. For example, the following code exports and imports a function:
// example.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.hello = factory();
}
}(this, function () {
function hello() {
console.log('Hello, world!');
}
return hello;
}));
// app.js
const hello = require('./example');
hello();
ECMAScript Modules (ESM)
ECMAScript Modules (ESM) are a module system built into the JavaScript language itself. They use the import
and export
keywords to define and load modules. ESM modules are loaded asynchronously, and they are executed in strict mode. ESM modules export a value, and that value becomes part of the module’s public interface. For example, the following code exports and imports a function:
// example.js
export function hello() {
console.log('Hello, world!');
}
// app.js
import { hello } from './example.js';
hello();
Here is a table to point out the difference between the aforementioned module systems.
Module System | Synchronous/Asynchronous | Syntax | Designed for | Export |
---|---|---|---|---|
CommonJS (CJS) | Synchronous | require() and module.exports | Node.js | Object |
Asynchronous Module Definition (AMD) | Asynchronous | define() | Browser | Object |
Universal Module Definition (UMD) | Both | Combination of require() and define() syntax | Both | Object |
ECMAScript Modules (ESM) | Asynchronous | import and export | Browser and Node.js | Value |
Conclusion
Choosing the right module system depends on the needs of your project. CommonJS is great for server-side code, while AMD is better suited for client-side code. UMD and ESM are more flexible, and can be used in both server-side and client-side code. By understanding the differences between these module systems, you can make an informed decision about which one to use in your projects.
Shivaraj Bakale
Diving deep from frontend waves to the backend caves 🌊🤠