This content originally appeared on DEV Community and was authored by SOVANNARO
Hey there, awesome devs! ๐ Have you ever wondered how Node.js handles files, streams, networking, and more without installing extra packages? The secret lies in built-in modulesโpowerful tools that come preloaded with Node.js! ๐
๐ What Are Built-in Modules?
Built-in modules are core modules that come with Node.js, so you donโt need to install them separately. Just require or import them, and youโre good to go! ๐
Hereโs how to import built-in modules:
๐น CommonJS (require)
const fs = require('fs');
๐น ES Modules (import)
import fs from 'fs';
Now, letโs explore some of the most useful modules! ๐
๐ฅ 1. fs
(File System) - Work with Files ๐
The fs
module allows you to read, write, update, and delete files.
๐ Read a File
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
โ๏ธ Write to a File
fs.writeFile('output.txt', 'Hello, Node.js!', (err) => {
if (err) throw err;
console.log('File written successfully!');
});
๐ฅ 2. path
- Work with File Paths ๐๏ธ
The path
module makes handling file paths easy and OS-independent.
๐ Get File Extension
const path = require('path');
console.log(path.extname('index.html')); // Output: .html
๐ Join Paths Correctly
console.log(path.join(__dirname, 'folder', 'file.txt'));
๐ฅ 3. http
- Create a Web Server ๐
The http
module lets you build simple web servers.
๐ Create a Basic Server
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!');
});
server.listen(3000, () => console.log('Server running on port 3000'));
Open http://localhost:3000/
in your browser and see it in action! ๐
๐ฅ 4. os
- Get System Information ๐ฅ๏ธ
The os
module provides system-related info like OS type, memory, and CPU details.
๐ฅ๏ธ Get OS Type
const os = require('os');
console.log(os.type()); // Output: Windows_NT, Linux, Darwin
๐พ Check Free Memory
console.log(os.freemem());
๐ฅ 5. events
- Work with Events ๐ฏ
The events
module allows you to create and handle custom events.
๐ฅ Create an Event Emitter
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
eventEmitter.on('greet', () => console.log('Hello, Developer!'));
eventEmitter.emit('greet');
๐ Final Thoughts
Node.js built-in modules make development faster and easier by providing powerful utilities right out of the box! Whether youโre handling files, paths, servers, or system info, Node.js has you covered! ๐ช
Stay tuned for the next article, where weโll explore even more Callback Pattern! ๐ฏ
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! ๐ป๐ฅ
This content originally appeared on DEV Community and was authored by SOVANNARO

SOVANNARO | Sciencx (2025-03-01T11:28:58+00:00) Built-in Modules in Node.js ๐. Retrieved from https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.