Exploring JavaScript Modules: Transitioning from CommonJS to ES6

Mitesh KukdejaMitesh Kukdeja
4 min read

JavaScript has evolved massively over the years, and modularization is a core part of writing maintainable, scalable code. This guide covers everything from CommonJS to ES6 modules, how tools like Babel make them compatible, and when to use what.


🔰 What is ES6 (aka ECMAScript 2015)?

ES6 (ECMAScript 2015) introduced modern JavaScript features—one of the most important being modules. This allowed developers to break code into reusable files and import/export them cleanly.

// user.js
export const name = 'Alice';
// app.js
import { name } from './user.js';
console.log(name); // Alice

📦 Introduction to Modules in JavaScript

Modules help developers:

  • Split code into reusable pieces

  • Avoid global variables

  • Maintain separation of concerns

JavaScript has two main module systems:

  • CommonJS (Node.js default)

  • ES6 Modules (native in browsers & modern tools)


🚀 ES6 Modules: import / export

ES6 modules use the import and export syntax:

// math.js
export function add(a, b) {
  return a + b;
}
// app.js
import { add } from './math.js';
console.log(add(2, 3)); // 5

You can also export default:

export default function greet() {
  console.log("Hello!");
}

And import like:

import greet from './greet.js';

📦 CommonJS Modules: require() / module.exports

CommonJS is the original module system used in Node.js:

// math.js
function subtract(a, b) {
  return a - b;
}
module.exports = subtract;
// app.js
const subtract = require('./math');
console.log(subtract(5, 3)); // 2

🧾 type: "module" in package.json

To use ES6 modules in Node.js, add this to your package.json:

{
  "type": "module"
}

This enables import/export syntax. Without it, Node will assume CommonJS.

📌 Note: File extensions are required (.js, .mjs) in ES6 modules.


⚔️ Differences Between import and require()

Featureimport (ES6)require() (CommonJS)
Syntax TypeStaticDynamic
Can be top-level only?✅ Yes❌ No (can be conditional)
File Extension Required?✅ Yes (.js)❌ No
Default in Node.js❌ No (needs type: module)✅ Yes
Tree-shaking support✅ Yes❌ No

🧠 When to Use CommonJS vs ES6 Modules

Use ES6 Modules:

  • In front-end apps (React, Vue, etc.)

  • In Node.js when targeting modern environments

  • For cleaner, standardized syntax

Use CommonJS:

  • In legacy Node.js code

  • For backward compatibility

  • In files where dynamic loading is needed


🛠 What is Babel?

Babel is a JavaScript compiler that converts modern ES6+ code into backward-compatible JavaScript that works in older browsers or environments.


🔄 How Babel Transpiles Modern JavaScript (ES6+)

For example:

// ES6
const greet = () => console.log('Hello');

Babel converts it to:

var greet = function() {
  return console.log('Hello');
};

Same goes for import/export:

import { sum } from './math.js';

becomes:

const { sum } = require('./math');

🚀 Why Babel Is Important for Module Compatibility

  • Ensures cross-browser support

  • Enables use of latest JS features

  • Allows writing ES6 modules and compiling to CommonJS when needed (e.g., for Node.js)


📦 Tools You Should Know

  • babel.config.js: Babel config file

  • @babel/preset-env: Transpiles code based on target environments

  • Webpack + Babel: Bundle + transpile your app for production

// babel.config.js
module.exports = {
  presets: ['@babel/preset-env']
};

🌍 Real-World Scenario: When to Use ES6 vs CommonJS

  • Use ES6 modules in any front-end app (e.g., React)

  • Use CommonJS if you're building a CLI tool for Node.js that needs to support older versions

  • Hybrid apps? Use Babel to bridge the gap!


📊 Compatibility Table (Node.js)

Node VersionES6 Module Support
12.xPartial (needs flag)
14.x✅ Stable with type: module
16.x+✅ Fully supported

🙏 Thank You!

Thank you for reading!

I hope you enjoyed this post. If you did, please share it with your network and stay tuned for more insights on software development. I'd love to connect with you on LinkedIn or have you follow my journey on HashNode for regular updates.

Happy Coding!
Mitesh Kukdeja

0
Subscribe to my newsletter

Read articles from Mitesh Kukdeja directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mitesh Kukdeja
Mitesh Kukdeja

Turning ideas into smooth, scalable mobile experiences — one line of code at a time. Hi, I’m Mitesh Kukdeja — a passionate React Native developer with 2+ years of hands-on experience building cross-platform apps that delight users and deliver results. From health and fitness platforms to job boards and music contest apps, I’ve helped bring a wide range of product visions to life. What sets me apart is my obsession with clean, reusable code and user-centric UI/UX. I specialize in React Native, TypeScript, Redux Toolkit, Firebase, and REST API integration—making sure every app I build is responsive, secure, and ready for scale. I’ve also deployed apps to both the Play Store and App Store, managing the full release cycle. My projects have included integrating real-time features like video conferencing (Agora), personalized push notifications, and advanced security implementations for enterprise clients like Godrej. Whether it’s debugging a performance bottleneck or designing a scalable component architecture, I’m all in. My goal is to keep solving meaningful problems through technology while collaborating with creative minds. I thrive in fast-paced environments where innovation and impact matter. If you’re building something exciting in mobile or looking for a tech partner who values quality and performance — let’s connect!