DEV Community

SOVANNARO
SOVANNARO

Posted on

ES Modules in Node.js: The Modern Way to Handle Modules ๐Ÿš€

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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Option 2: Set "type": "module" in package.json

{
  "type": "module"
}
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ค 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;
Enter fullscreen mode Exit fullscreen mode

Importing:

// file: app.js
import { greet, age } from './utils.js';
console.log(greet('Bob')); // Output: Hello, Bob!
console.log(age); // Output: 25
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Default Export

Export a single value:

// file: math.js
export default function add(a, b) {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Importing:

// file: app.js
import add from './math.js';
console.log(add(5, 3)); // Output: 8
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ 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!
Enter fullscreen mode Exit fullscreen mode

โœ… Use import instead:

import myModule from './utils.js';
Enter fullscreen mode Exit fullscreen mode

โŒ Forgetting .js extension in imports

import { greet } from './utils'; // โŒ Error in Node.js!
Enter fullscreen mode Exit fullscreen mode

โœ… Always include .js:

import { greet } from './utils.js';
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ 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! ๐Ÿ’ป๐Ÿ”ฅ

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs