Hey awesome devs! π Ever wondered how to structure your modules properly in Node.js? There are different ways to import and export modules, and choosing the right pattern can improve your code readability and maintainability. In this blog, weβll explore common import/export patterns in a fun and easy way! π
π§ Why Do We Need Import and Export?
Node.js follows a modular approach, meaning we can split our code into smaller, reusable pieces instead of writing everything in one file. This makes our code cleaner, more maintainable, and easier to debug.
There are two main module systems in Node.js:
-
CommonJS (CJS) β Uses
require()
andmodule.exports
. -
ES Modules (ESM) β Uses
import
andexport
.
Letβs explore these patterns with examples! π
π₯ CommonJS (CJS) β The Default in Node.js
π Example: Exporting with module.exports
// file: math.js
function add(a, b) {
return a + b;
}
module.exports = add; // Exporting a single function
π Example: Importing with require()
// file: app.js
const add = require('./math');
console.log(add(5, 3)); // Output: 8
π§ How It Works?
- We export the
add
function usingmodule.exports
. - We import it using
require('./math')
. - Best for: Older Node.js projects, backend applications.
π₯ Exporting Multiple Items in CommonJS
// file: utils.js
function greet(name) {
return `Hello, ${name}!`;
}
function farewell(name) {
return `Goodbye, ${name}!`;
}
module.exports = { greet, farewell }; // Exporting multiple functions
π Importing Multiple Items
// file: app.js
const utils = require('./utils');
console.log(utils.greet('Alice')); // Output: Hello, Alice!
console.log(utils.farewell('Bob')); // Output: Goodbye, Bob!
π ES Modules (ESM) β The Modern Way
ES Modules (ESM) is the new standard and is recommended for modern applications. It uses import
and export
instead of require()
and module.exports
.
π Example: Named Exports
// file: math.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
π Importing Named Exports
// file: app.js
import { add, subtract } from './math.js';
console.log(add(10, 5)); // Output: 15
console.log(subtract(10, 5)); // Output: 5
π§ How It Works?
- We export multiple functions using
export function
. - We import only the ones we need using
{}
.
π₯ Exporting Everything as a Single Object (ESM)
// file: utils.js
function greet(name) {
return `Hello, ${name}!`;
}
function farewell(name) {
return `Goodbye, ${name}!`;
}
export { greet, farewell }; // Named exports
π Importing Everything as an Object
// file: app.js
import * as utils from './utils.js';
console.log(utils.greet('Alice')); // Output: Hello, Alice!
console.log(utils.farewell('Bob')); // Output: Goodbye, Bob!
π§ Best for: Grouping functions under one namespace.
β Choosing the Right Import/Export Pattern
Pattern | Syntax | Best for |
---|---|---|
CommonJS (CJS) |
module.exports , require()
|
Traditional Node.js apps |
ES Modules (ESM) |
export , import
|
Modern JavaScript apps |
Named Exports | export {} |
Exporting multiple items |
Default Exports | export default |
Exporting a single item |
Import Everything | import * as name |
Grouping functions |
π― Final Thoughts
Understanding import/export patterns helps you write cleaner and more scalable Node.js applications. Whether you use CommonJS or ES Modules, knowing when to use each method will level up your coding skills! π
This is just the beginning! In the next article, weβll explore Module.Exports vs Exportsβ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)