Hey awesome devs! π Have you ever wondered about ES Modules (ESM) in Node.js and how they differ from CommonJS (require
and module.exports
)?
Well, youβre in the right place! Today, weβre diving into ES Modules, the modern way to import and export in JavaScript. By the end of this post, youβll understand ES Modules like a pro! π
π§ What Are ES Modules (ESM)?
ES Modules (ECMAScript Modules) are the standard JavaScript module system that allows you to import and export code cleanly and efficiently. Unlike CommonJS, which is synchronous, ESM supports asynchronous loading, making it great for modern applications.
πΉ Why Use ES Modules?
β
Better performance (supports async imports).
β
Tree-shaking (removes unused code to reduce file size).
β
More readable syntax (import/export
instead of require
).
β
Standardized across browsers & Node.js.
π₯ How to Use ES Modules in Node.js
1οΈβ£ Enable ES Modules in Node.js
To use ES Modules in Node.js, you have two options:
πΉ Option 1: Use .mjs
extension
node myfile.mjs
πΉ Option 2: Set "type": "module"
in package.json
{
"type": "module"
}
Now, all .js
files in your project will be treated as ES Modules.
π₯ Importing in ES Modules
Instead of require()
, ES Modules use import
:
// Importing a named export
import { greet } from './utils.js';
greet('Alice'); // Output: Hello, Alice!
π€ Exporting in ES Modules
There are two ways to export in ESM:
1οΈβ£ Named Exports
Export multiple functions or variables:
// file: utils.js
export function greet(name) {
return `Hello, ${name}!`;
}
export const age = 25;
Importing:
// file: app.js
import { greet, age } from './utils.js';
console.log(greet('Bob')); // Output: Hello, Bob!
console.log(age); // Output: 25
2οΈβ£ Default Export
Export a single value:
// file: math.js
export default function add(a, b) {
return a + b;
}
Importing:
// file: app.js
import add from './math.js';
console.log(add(5, 3)); // Output: 8
π Default exports donβt need curly braces ({}
) when importing!
β οΈ Common Mistakes to Avoid
β Mixing CommonJS and ES Modules
const myModule = require('./utils.js'); // β Wonβt work with ESM!
β
Use import
instead:
import myModule from './utils.js';
β Forgetting .js
extension in imports
import { greet } from './utils'; // β Error in Node.js!
β
Always include .js
:
import { greet } from './utils.js';
π When to Use ES Modules vs CommonJS?
Feature | CommonJS (require ) |
ES Modules (import/export ) |
---|---|---|
Default in Node.js | β Yes | β No (requires setup) |
Browser Support | β No | β Yes |
Asynchronous Loading | β No | β Yes |
Tree-Shaking Support | β No | β Yes |
If youβre building modern applications or want better performance, ES Modules is the way to go! π
π― Final Thoughts
ES Modules are the future of JavaScript! They are cleaner, faster, and standardized across both browsers and Node.js. Start using them today to write modern, efficient code! πͺ
This is just the beginning! In the next article, weβll explore Importing JSON and Watch Modeβstay tuned! π―
If you found this blog helpful, make sure to follow me on GitHub π github.com/sovannaro and drop a β. Your support keeps me motivated to create more awesome content! π
Happy coding! π»π₯
Top comments (0)