π¦ Day 4: Node.js Core Modules β path, os, and events

Table of contents
- π¨βπ« What Youβll Learn Today:
- πΉ What Are Core Modules?
- π 1. path Module β Handling File Paths
- π» 2. os Module β System Info from Node.js
- π 3. events Module β Handling Custom Events
- πΌ Real-Life Use Case (Mini Project Example)
- β FAQs β Common Doubts
- π Practice Tasks for You
- β Summary
- π Stay Connected
π¨βπ« What Youβll Learn Today:
What are core modules in Node.js?
How to use the
path
moduleHow the
os
module helps with system infoHow to work with events using the
events
moduleReal-life practical examples
FAQs to clear your doubts
πΉ What Are Core Modules?
Core modules are built-in tools provided by Node.js.
You don't need to install them separately β just use require()
to include them in your file.
Today weβll learn 3 important ones:
path
β To work with file and folder pathsos
β To get system-related informationevents
β To create and handle custom events
π 1. path
Module β Handling File Paths
The path
module is used to build and work with file and folder paths across different operating systems (Windows, Linux, macOS).
π Example 1: Joining Paths
const path = require('path');
const fullPath = path.join(__dirname, 'files', 'info.txt');
console.log('Full Path:', fullPath);
β
path.join()
safely combines folders/files into one full path.
π Example 2: Get File Name and Extension
const filePath = '/home/user/app.js';
console.log('File Name:', path.basename(filePath)); // app.js
console.log('Extension:', path.extname(filePath)); // .js
π Useful for:
Logging file uploads
File processing
Organizing folder structures
π» 2. os
Module β System Info from Node.js
The os
module gives you information about the operating system you are working on.
π Example: Basic OS Info
const os = require('os');
console.log('OS Type:', os.type()); // e.g. Windows_NT
console.log('Platform:', os.platform()); // e.g. win32
console.log('Free Memory:', os.freemem()); // in bytes
console.log('Home Directory:', os.homedir());
π Example: System Uptime
console.log('System Uptime (seconds):', os.uptime());
β Useful in:
Monitoring apps
Logging server behavior
Displaying server dashboard info
π 3. events
Module β Handling Custom Events
The events
module allows you to create your own events and respond to them β just like clicking a button in the browser triggers an event.
π Step-by-Step Example:
const EventEmitter = require('events'); // Import
const emitter = new EventEmitter(); // Create instance
// Listener
emitter.on('userRegistered', (name) => {
console.log(`Welcome, ${name}! Your account is ready.`);
});
// Trigger (emit) the event
emitter.emit('userRegistered', 'Payal');
β Output:
Welcome, Payal! Your account is ready.
π§ This is how backend apps handle:
Login success events
New order placed events
Email sent notifications
Custom logs
πΌ Real-Life Use Case (Mini Project Example)
Letβs create a simple event system for file download log:
const fs = require('fs');
const EventEmitter = require('events');
const emitter = new EventEmitter();
// Event listener
emitter.on('fileDownloaded', (fileName) => {
fs.appendFileSync('download-log.txt', `Downloaded: ${fileName}\n`);
console.log(`${fileName} logged successfully.`);
});
// Simulate file download
emitter.emit('fileDownloaded', 'resume.pdf');
β
This logs every file download into download-log.txt
.
β FAQs β Common Doubts
1) Why do we use path.join()
instead of string concatenation like __dirname + '/folder/file.txt'
?
β
path.join()
is cross-platform and automatically adds the correct separator (/
or \
) depending on OS.
Manual concatenation may break your code on other systems.
2) Whatβs the difference between os.type()
and os.platform()
?
Method | What It Returns |
os.type() | OS name like Windows_NT , Linux |
os.platform() | Platform identifier like win32 , darwin , linux |
Use both for debugging or logging system environments.
3) Can we create multiple custom events using the events
module?
β
Yes! You can create as many custom events as needed.
Each one should have a listener (.on
) and emitter (.emit
).
emitter.on('login', () => console.log('User logged in'));
emitter.emit('login');
4) Do I need to install path
, os
, or events
?
β No need to install β these are core modules in Node.js.
Just import using require()
like:
const path = require('path');
π Practice Tasks for You
Use
path
to get the name and extension of a file.Use
os
to log current system memory and platform.Create a custom event called
userDeleted
and print a goodbye message.Log system uptime using
os.uptime()
.
β Summary
Module | Purpose | Real-Life Use |
path | Work with file/folder paths | File storage, uploads, project setup |
os | Get info about the system/OS | Monitoring tools, dashboards |
events | Custom event handling | Notifications, logs, app actions |
You're now ready to use system-level power tools in Node.js! π―
π Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Node JS notes, tutorials, and project ideas β π© Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, π₯ Donβt forget to subscribe to my YouTube channel β Knowledge Factory 22 β for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. π
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! π