Home Coding & Scripting Working with Core Node.js Modules (fs, path, http)

Working with Core Node.js Modules (fs, path, http)

268
0

When you install Node.js, you don’t just get the ability to run JavaScript outside the browser. You also get a set of core modules — built-in tools that let you interact with your computer and the internet. Unlike external packages you install with npm, these modules come ready to use.

💡 Before diving in: if you don’t already have Node.js installed, you can start with the simple setup in Getting Started with Node.js: Setup and Hello World. For developers who want the professional way to manage Node.js versions, check out my guide: Node Version Manager (nvm): A Developer’s Guide to Installing and Managing Node.js Versions.

👉 Tip: For each example below, make sure any mentioned files or folders (like example.txt or files/data.json) exist in your project before running the code.

We’ll look at three of the most useful core modules: fs, path, and http.


📂 The fs Module (File System)

The fs module lets you work with files — reading, writing, and updating them.

Example: Reading a File

const fs = require('fs');

// Make sure example.txt exists in the same folder
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(data);
});

This reads the contents of example.txt and prints it to the console.


🛤️ The path Module

The path module helps you work with file and folder paths in a way that works across operating systems.

Example: Joining Paths

const path = require('path');

// Assumes there's a folder called "files" with a file named data.json
const fullPath = path.join(__dirname, 'files', 'data.json');
console.log(fullPath);

This prints the full path to data.json, with the correct separators for your system.


🌐 The http Module

The http module lets you create web servers. With just a few lines of code, you can respond to requests in a browser.

Example: A Simple Web Server

const http = require('http');

// No extra files needed — just run this script
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from Node.js!');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000');
});

Open your browser at http://localhost:3000 to see the response.


✅ Key Takeaway

  • fs: Work with files (read/write).
  • path: Safely handle file paths.
  • http: Create web servers.

These modules are the foundation of many Node.js projects. They let JavaScript interact with your computer and the web without needing any extra tools.

Previous articleThe Event Loop and Asynchronous Programming in Node.js
Next articleUnderstanding the Node.js Runtime
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here