
Node.js Tutorial – Day 4: Working with File System in Node.js
Welcome back to our Node.js Tutorial series! So far, we’ve covered:
- Day 1: Introduction to Node.js
- Day 2: Node.js Modules
- Day 3: Node.js Event Loop
Today in Day 4, we’ll explore one of the most useful built-in modules – the File System (fs module).
The fs module allows Node.js developers to interact with files and directories, making it possible to build apps that can store, read, and manage data efficiently.
Why is File System Important in Node.js?
Almost every application requires file handling:
- Saving user data to a file
- Reading configuration files
- Writing logs for debugging
- Handling uploads and downloads
Node.js provides the fs module to perform all these operations asynchronously (non-blocking) or synchronously (blocking).
Importing File System Module
Before we start, let’s import the module:
const fs = require('fs');
This single line gives us access to powerful functions for file handling.
Reading Files in Node.js
You can read files in two ways:
1. Asynchronous Read
fs.writeFile('output.txt', 'Hello, Node.js Tutorial!', (err) => {
if (err) throw err;
console.log("File has been written!");
});
Here, the program does not wait for the file to be read. Instead, it continues execution and shows file content once ready.
2. Synchronous Read
const data = fs.readFileSync('example.txt', 'utf8');
console.log("File content:", data);
This blocks the execution until the file is read.
Writing Files in Node.js
Example: Asynchronous Write
fs.writeFile('output.txt', 'Hello, Node.js Tutorial!', (err) => {
if (err) throw err;
console.log("File has been written!");
});
Example: Synchronous Write
fs.writeFileSync('output.txt', 'Learning Node.js is fun!');
console.log("File written successfully!");
Appending to a File
Instead of overwriting, you can add content to an existing file.
fs.appendFile('output.txt', '\nNew line added!', (err) => {
if (err) throw err;
console.log("Content appended!");
});
Deleting Files
fs.unlink('output.txt', (err) => {
if (err) throw err;
console.log("File deleted!");
});
Working with Directories
Node.js also allows managing directories:
- Create Directory
fs.mkdir('myFolder', (err) => {
if (err) throw err;
console.log("Folder created!");
});
- Read Directory
fs.readdir('.', (err, files) => {
if (err) throw err;
console.log("Files in directory:", files);
});
- Remove Directory
fs.rmdir('myFolder', (err) => {
if (err) throw err;
console.log("Folder removed!");
});
Mini-Project: File-Based Notes App
Let’s build a small Notes App using the fs module.
Step 1: Create notes.js
const fs = require('fs');
// Add a new note
function addNote(title, content) {
const note = `Title: ${title}\nContent: ${content}\n\n`;
fs.appendFileSync('notes.txt', note);
console.log("Note added!");
}
// Read all notes
function readNotes() {
const data = fs.readFileSync('notes.txt', 'utf8');
console.log("Your Notes:\n", data);
}
// Delete all notes
function clearNotes() {
fs.writeFileSync('notes.txt', '');
console.log("All notes cleared!");
}
module.exports = { addNote, readNotes, clearNotes };
Step 2: Create app.js
const notes = require('./notes');
notes.addNote("Shopping List", "Buy milk, bread, and eggs");
notes.addNote("Reminder", "Meeting at 10 AM tomorrow");
notes.readNotes();
notes.clearNotes();
Output:
Note added!
Note added!
Your Notes:
Title: Shopping List
Content: Buy milk, bread, and eggs
Title: Reminder
Content: Meeting at 10 AM tomorrow
All notes cleared!
This small project shows how to create, read, and manage data using the File System module.
Best Practices for File Handling
- Always handle errors when working with files.
- Prefer asynchronous methods for better performance.
- Avoid blocking the Event Loop with synchronous methods unless necessary.
- Keep file operations modular and reusable.
File System Official Documentation
Conclusion
In this lesson of our Node.js Tutorial, we covered:
- Importing the File System (
fs
) module - Reading, writing, appending, and deleting files
- Working with directories
- A mini-project: File-Based Notes App
The File System module is one of the most powerful features in Node.js, giving you full control over data storage and file handling.
👉 Up Next: Day 5 – Node.js HTTP Module and Creating a Server