DEV Community

SOVANNARO
SOVANNARO

Posted on

2

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 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');
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή ES Modules (import)

import fs from 'fs';
Enter fullscreen mode Exit fullscreen mode

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);
});
Enter fullscreen mode Exit fullscreen mode

✍️ Write to a File

fs.writeFile('output.txt', 'Hello, Node.js!', (err) => {
  if (err) throw err;
  console.log('File written successfully!');
});
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Join Paths Correctly

console.log(path.join(__dirname, 'folder', 'file.txt'));
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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'));
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

πŸ’Ύ Check Free Memory

console.log(os.freemem());
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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');
Enter fullscreen mode Exit fullscreen mode

πŸš€ 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! πŸ’»πŸ”₯

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more