π¦ Day 2: Node.js Modules β Core Modules, Custom Modules, require and exports

Table of contents
- π¨βπ« What Youβll Learn Today:
- β What is a Module?
- π Types of Node.js Modules
- π§ Core Modules β Built-in Features
- π§© Custom Modules β Your Own Code
- π module.exports and require() in Detail
- π― Real-Life Use Case: Utilities Module
- π‘ Why Professionals Use Modules
- π Practice Tasks
- π Summary
π¨βπ« What Youβll Learn Today:
What is a module in Node.js?
Core vs Custom Modules
How to use
require
andexports
Real-life examples
Practical tasks for hands-on learning
β What is a Module?
A module in Node.js is like a small building block of code that performs a specific task.
You can think of it as a separate file that contains reusable logic.
π¦ Instead of writing all code in one file, we divide it into modules and import them wherever needed.
π Types of Node.js Modules
Type | Example | Description |
Core Modules | fs , path , os , http | Built-in modules, come with Node.js |
Custom Modules | Your own files | Created by you for project-specific logic |
Third-Party | express , lodash , etc. | Installed via npm (weβll cover this later) |
π§ Core Modules β Built-in Features
Node.js gives us many useful modules out of the box.
Letβs explore 3 common ones:
1. fs
(File System)
Used to read/write files.
const fs = require('fs');
fs.writeFileSync('hello.txt', 'Hello from Node.js!');
β
Output: Creates a file named hello.txt
with content inside.
2. path
Used to work with file paths.
const path = require('path');
const filePath = path.join(__dirname, 'hello.txt');
console.log(filePath);
β
Output: Full path of hello.txt
3. os
Gives info about your computer system.
const os = require('os');
console.log("OS type:", os.type());
console.log("Free memory:", os.freemem());
β Output: Your system type and available memory.
π§© Custom Modules β Your Own Code
You can create your own modules in separate files and use them anywhere using require()
.
π Example: Create a Calculator Module
β€ Step 1: Create a file calculator.js
// calculator.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
// Export functions to be used in other files
module.exports = {
add,
subtract
};
β€ Step 2: Create a file app.js
to use this module
// app.js
const calc = require('./calculator');
console.log("Addition:", calc.add(5, 3));
console.log("Subtraction:", calc.subtract(10, 4));
π’ Output:
Addition: 8
Subtraction: 6
π‘ require('./calculator')
is used to import your custom module.
π module.exports
and require()
in Detail
Concept | Description |
module.exports | Used in a module to expose functions/variables so they can be used outside |
require() | Used to import the content of one module into another |
π You can export a single function, object, or even class.
π― Real-Life Use Case: Utilities Module
Suppose you're building a blog backend, and you need a function to format the current date.
β€ utils.js
function getCurrentDate() {
const date = new Date();
return date.toLocaleDateString();
}
module.exports = { getCurrentDate };
β€ blog.js
const utils = require('./utils');
console.log("Post Date:", utils.getCurrentDate());
β This makes your code reusable, clean, and modular.
π‘ Why Professionals Use Modules
Code Reusability
Better Structure in Large Projects
Easier Maintenance
Clean Separation of Concerns
β In a real backend project, your code will be split into modules like:
routes
controllers
models
utilities
services
π Practice Tasks
Create a module
math.js
with multiply and divide functionsUse it in another file and print results
Create a custom module
info.js
that exports your name, role, and institute
π Summary
What You Learned | Why It Matters |
Core Modules (fs , os ) | Built-in power tools |
Custom Modules | Reusable, clean, organized code |
module.exports , require | Share code across files |
Real-world usage | Forms the base of large backend projects |
π You're now ready to build modular, scalable backend apps!
Would you like me to continue with Day 3: File System module (fs
) β reading and writing files in the same detailed format?
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: π In-depth explorations of emerging technologies π‘ Practical tutorials and how-to guides π§Insights on software development best practices πReviews of the latest tools and frameworks π‘ Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letβs connect and grow together! π