Built-in Modules in Node.js ๐Ÿš€

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…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Built-in Modules in Node.js ๐Ÿš€." SOVANNARO | Sciencx - Saturday March 1, 2025, https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/
HARVARD
SOVANNARO | Sciencx Saturday March 1, 2025 » Built-in Modules in Node.js ๐Ÿš€., viewed ,<https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/>
VANCOUVER
SOVANNARO | Sciencx - » Built-in Modules in Node.js ๐Ÿš€. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/
CHICAGO
" » Built-in Modules in Node.js ๐Ÿš€." SOVANNARO | Sciencx - Accessed . https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/
IEEE
" » Built-in Modules in Node.js ๐Ÿš€." SOVANNARO | Sciencx [Online]. Available: https://www.scien.cx/2025/03/01/built-in-modules-in-node-js-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» Built-in Modules in Node.js ๐Ÿš€ | SOVANNARO | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.