Modules in JavaScript: How to Organize Your Code with ES Modules
Learn how to use ES Modules in JavaScript to efficiently organize your code in a reusable way. Check out practical examples and essential tips!

If you've ever found yourself lost in a JavaScript codebase full of scattered functions, it's time to learn about <strong>ES Modules</strong>.
This native JavaScript feature allows you to split your code into smaller, reusable files, making your application more organized and easier to maintain.
In this article, we'll explore everything about JavaScript modules, how to use them, and why they are essential for any modern developer.
Shall we begin?
What Are ES Modules?
ES Modules (or simply ECMAScript modules) were introduced inECMAScript 6 (ES6) to enable native code modularization in JavaScript.
Before modules, developers relied on libraries like CommonJS (used in Node.js) or RequireJS to manage dependencies.
With the arrival of ES Modules, we can import and export functions, objects, and classes directly in the browser without the need for external tools.
📂 How Do Import and Export Work?
JavaScript modules operate based on two main concepts: exporting and importing.
Exporting Modules
To make code available to other files, we use the <code>export</code> keyword. Here's a simple example:
// file: math.js
export function sum(a, b) {
return a + b;
}
export const PI = 3.14159;
We can also use export default
, which allows exporting only one value per module:
// file: greeting.js
export default function greeting(name) {
return `Hello, ${name}!`;
}
Importing Modules
Now that we've exported, we can import these functions and constants into another file:
// file: app.js
import { sum, PI } from './math.js';
console.log(sum(5, 3)); // 8
console.log(PI); // 3.14159
To import a module exported as default, we use:
// file: app.js
import greeting from './greeting.js';
console.log(greeting('Maria')); // "Hello, Maria!"
Advantages of Using ES Modules
Advantage | Description |
---|---|
Organization | Helps separate code into smaller, reusable parts. |
Reusability | Avoids code duplication, promoting function and component reuse. |
Better Maintenance | Makes modifications easier, as each module has a clear responsibility. |
Native Support | Modern browsers support ES Modules without the need for external tools. |
Precautions When Using Modules
Despite their advantages, keep in mind a few points:
- ES Modules are loaded asynchronously, so there may be a slight execution delay.
- When referencing files, always include the .js extension, as browsers might not recognize them automatically.
- If running a project locally, use an HTTP server, as some browsers block module imports via
file://
.
Browser Compatibility
ES Modules are supported by most modern browsers. See the compatibility table:
Browser | Support |
---|---|
Chrome | ✔️ Yes |
Firefox | ✔️ Yes |
Edge | ✔️ Yes |
Safari | ✔️ Yes |
Internet Explorer | ❌ No |
Conclusion
ES Modules represent a significant evolution in how we write JavaScript code. With them, we can better structure our projects, making them more organized, reusable, and easier to maintain.
If you're not using modules in your code yet, start now! It will make a huge difference in your application's development.
💡 Do you already use ES Modules in your daily work? Share your experience in the comments!