๐ Day 6: CommonJS vs ES Modules โ require() vs import
Learn how Node.js modules work with CommonJS and ES Modules, understand require() vs import, and know when to use each module system in modern Node.js applications.
Node.js Mastery Series by CodeWithPatil โ Day 6
๐ Table of Contents
๐ Introduction
๐ Prerequisites
๐ค What is a Module System?
๐ Why Do We Need a Module System?
โ๏ธ How Module Systems Work
๐ฆ CommonJS
๐ ES Modules
๐ป Practical Examples
โ๏ธ CommonJS vs ES Modules
๐
require()vsimport๐
module.exportsvsexportโ๏ธ How to Enable ES Modules
๐ Real-World Use Cases
โ ๏ธ Common Mistakes
๐ก Best Practices
๐ง Interview Questions
โ Key Takeaways
๐ What's Next?
๐ CodeWithPatil
๐ Introduction
In Day 5 โ Understanding Node.js Modules, we explored:
Core Modules
Local Modules
Third-Party Modules
require()Creating reusable modules
However, there is another important concept every Node.js developer must understand:
Node.js supports multiple module systems.
The two most common module systems are:
CommonJS (CJS)
const fs = require("fs");
ES Modules (ESM)
import fs from "fs";
Both statements import modules, but they belong to different module systems.
This naturally raises an important question:
What is the difference between
require()andimport?
Let's understand everything step by step. ๐
๐ Prerequisites
Before reading this article, make sure you're familiar with:
โ Basic JavaScript Functions
โ Objects and Arrays
โ Node.js Fundamentals
โ Node.js Modules (Day 5)
โ
package.json
๐ค What is a Module System?
A module system defines how JavaScript code can be:
Organized
Split into multiple files
Exported
Imported
Reused
Shared across applications
Imagine your application contains:
app.js
math.js
user.js
database.js
Instead of writing all logic inside app.js, you can keep related functionality in separate files and import only what you need.
That's exactly what a module system helps us achieve.
Node.js primarily supports two module systems:
CommonJS (CJS)
ES Modules (ESM)
๐ Why Do We Need a Module System?
Without modules:
function add() {}
function subtract() {}
function connectDB() {}
function loginUser() {}
function sendEmail() {}
As applications grow, maintaining such files becomes difficult.
Benefits of Modules
โ Better organization
โ Code reusability
โ Easier maintenance
โ Improved scalability
โ Team collaboration
โ Cleaner architecture
๐ฆ CommonJS (CJS)
CommonJS is the traditional module system used by Node.js.
It uses:
require()
for importing modules and:
module.exports
for exporting modules.
Example:
const math = require("./math");
module.exports = math;
๐ Understanding require()
require() loads a module and returns whatever that module exports.
const path = require("path");
console.log(path.join("users", "data"));
Output:
users/data
๐ค Understanding module.exports
math.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = {
add,
subtract,
};
app.js
const math = require("./math");
console.log(math.add(10, 5));
console.log(math.subtract(10, 5));
Output:
15
5
Flow:
math.js
โ
module.exports
โ
app.js
โ
require("./math")
๐ What Are ES Modules (ESM)?
ES Modules are the official JavaScript module system introduced by ECMAScript.
Instead of:
require()
they use:
import
Instead of:
module.exports
they use:
export
Example:
import { add } from "./math.js";
export function add(a, b) {
return a + b;
}
๐ฅ Understanding import
import { add } from "./math.js";
console.log(add(10, 5));
Flow:
import
โ
Loads exported functionality
โ
from another file
๐ค Understanding export
Named Export
export function add(a, b) {
return a + b;
}
Import:
import { add } from "./math.js";
Default Export
export default function greet(name) {
return `Hello ${name}`;
}
Import:
import greet from "./greet.js";
console.log(greet("Developer"));
Output:
Hello Developer
๐ป Practical Examples
๐ฆ CommonJS Practical Example
Project Structure
commonjs-demo/
โ
โโโ app.js
โโโ calculator.js
โโโ package.json
calculator.js
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
module.exports = {
multiply,
divide,
};
app.js
const calculator = require("./calculator");
console.log(calculator.multiply(5, 4));
console.log(calculator.divide(20, 5));
Output:
20
4
๐ ES Modules Practical Example
Project Structure
esm-demo/
โ
โโโ app.js
โโโ calculator.js
โโโ package.json
calculator.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
app.js
import { add, subtract } from "./calculator.js";
console.log(add(10, 5));
console.log(subtract(10, 5));
Output:
15
5
โ๏ธ How to Enable ES Modules
Option 1: "type": "module"
package.json
{
"name": "esm-demo",
"version": "1.0.0",
"type": "module"
}
Option 2: .mjs
app.mjs
calculator.mjs
Example:
import { add } from "./calculator.mjs";
โ๏ธ CommonJS vs ES Modules
| Feature | CommonJS | ES Modules |
|---|---|---|
| Import | require() |
import |
| Export | module.exports |
export |
| Default Export | CommonJS Pattern | export default |
| Extension | .js |
.js, .mjs |
| Standard | โ No | โ Yes |
| Browser Support | โ No | โ Yes |
๐ require() vs import
CommonJS
const express = require("express");
ES Modules
import express from "express";
๐ module.exports vs export
CommonJS
function add(a, b) {
return a + b;
}
module.exports = {
add,
};
Import:
const { add } = require("./math");
ES Modules
export function add(a, b) {
return a + b;
}
Import:
import { add } from "./math.js";
๐ Real-World Use Cases
Building REST APIs
Working with Express.js
Creating npm packages
Building reusable libraries
Migrating old Node.js applications
Working with TypeScript projects
โ ๏ธ Common Mistakes
1. Mixing require() and import
const fs = require("fs");
import express from "express";
Avoid mixing unless you understand project configuration.
2. Forgetting "type": "module"
{
"type": "module"
}
3. Forgetting File Extensions
โ Wrong
import { add } from "./math";
โ Correct
import { add } from "./math.js";
4. Using Curly Braces Incorrectly
Named Export:
export function add() {}
import { add } from "./math.js";
Default Export:
export default function add() {}
import add from "./math.js";
๐ก Best Practices
โ Use ES Modules in new projects
โ Learn both CJS and ESM
โ Keep imports organized
โ Understand named vs default exports
โ Avoid unnecessary mixing
โ Use small reusable modules
๐ง Interview Questions
What is CommonJS?
A Node.js module system that uses require() and module.exports.
What are ES Modules?
The official JavaScript module system that uses import and export.
Difference between require() and import?
require() belongs to CommonJS. import belongs to ES Modules.
How do you enable ESM?
Using:
{
"type": "module"
}
or .mjs files.
โ Key Takeaways
โ CommonJS uses:
require()
module.exports
โ ES Modules use:
import
export
โ ESM is the modern JavaScript standard.
โ Learn both systems because real-world projects use both.
Memory Trick
CommonJS
โโโ require()
โโโ module.exports
ES Modules
โโโ import
โโโ export
๐ What's Next?
Day 7 โ Node.js Global Objects: global, __dirname, and __filename
You'll learn:
The
globalobject__dirname__filenameCommonJS vs ESM differences
Practical examples
Real-world use cases
See you in Day 7! ๐
๐ Follow CodeWithPatil
If you enjoy practical, real-world tutorials on Backend Development, Cloud Computing, DevOps, and modern software engineering, follow CodeWithPatil for project-based learning.
๐ฌ Feedback & Support
Have a question, found an issue, or have suggestions for future topics?
Leave a comment below. I'd love to hear from you. Your feedback helps improve future articles and supports the learning community.
โญ If you found this article helpful, leave a reaction and share it with your developer friends.
Happy Coding! ๐
About the author
- # Hi, I'm Sumit Patil ๐ a Senior Backend Software Engineer with 7+ years of experience designing scalable backend systems and cloud-native applications. I specialize in Node.js, NestJS, TypeScript, GraphQL, PostgreSQL, MongoDB, AWS, Microservices, and DevOps. ๐ Smart Gujarat for New India Hackathon Winner
Comments
Join the discussionNo comments yet. Be the first to comment.

