π Day 10: Mini Project β Create a Local File Manager in Node.js

Table of contents
- π― Project Goal
- π¦ Tools Used
- π Project Structure
- π Step-by-Step Project Setup
- β Step 1: Create the Project Folder
- β Step 2: Create fileManager.js
- βοΈ Step 3: Add Required Modules
- β Step 4: Create Folder Function
- β Step 5: Create File Inside Folder
- β Step 6: Read File Content
- β Step 7: Update File Content
- β Step 8: Delete File
- β Step 9: Delete Folder
- π§ͺ Step 10: Call Functions to Test
- π§ What Students Will Learn
- β Sample Errors & How We Handled Them
- πͺ Suggested Student Tasks
- β Summary
- π Stay Connected
π― Project Goal
Create a CLI-based (Command Line Interface) File Manager that allows users to:
Create a folder
Create a file inside that folder
Read the file
Update the file
Delete the file or folder
All operations must include proper error handling.
π¦ Tools Used
Node.js
fs/promises
modulepath
moduleBasic knowledge of
async/await
andtry...catch
π Project Structure
local-file-manager/
βββ fileManager.js <-- All project logic here
π Step-by-Step Project Setup
β Step 1: Create the Project Folder
mkdir local-file-manager
cd local-file-manager
β
Step 2: Create fileManager.js
echo. > fileManager.js
βοΈ Step 3: Add Required Modules
// fileManager.js
const fs = require('fs').promises;
const path = require('path');
β Step 4: Create Folder Function
async function createFolder(folderName) {
try {
const folderPath = path.join(__dirname, folderName);
await fs.mkdir(folderPath);
console.log(`β
Folder '${folderName}' created successfully.`);
} catch (err) {
if (err.code === 'EEXIST') {
console.error("β Folder already exists.");
} else {
console.error("β Failed to create folder:", err.message);
}
}
}
π Explanation:
mkdir()
creates the folderpath.join()
ensures correct folder pathErrors like βfolder already existsβ are caught using
err.code
β Step 5: Create File Inside Folder
async function createFile(folderName, fileName, content) {
try {
const filePath = path.join(__dirname, folderName, fileName);
await fs.writeFile(filePath, content);
console.log(`β
File '${fileName}' created inside '${folderName}'.`);
} catch (err) {
console.error("β Error creating file:", err.message);
}
}
β Step 6: Read File Content
async function readFile(folderName, fileName) {
try {
const filePath = path.join(__dirname, folderName, fileName);
const data = await fs.readFile(filePath, 'utf-8');
console.log(`π File Content:\n${data}`);
} catch (err) {
console.error("β Error reading file:", err.message);
}
}
β Step 7: Update File Content
async function updateFile(folderName, fileName, newContent) {
try {
const filePath = path.join(__dirname, folderName, fileName);
await fs.appendFile(filePath, `\n${newContent}`);
console.log("β
File updated successfully.");
} catch (err) {
console.error("β Error updating file:", err.message);
}
}
β Step 8: Delete File
async function deleteFile(folderName, fileName) {
try {
const filePath = path.join(__dirname, folderName, fileName);
await fs.unlink(filePath);
console.log("ποΈ File deleted successfully.");
} catch (err) {
console.error("β Error deleting file:", err.message);
}
}
β Step 9: Delete Folder
async function deleteFolder(folderName) {
try {
const folderPath = path.join(__dirname, folderName);
await fs.rmdir(folderPath);
console.log("ποΈ Folder deleted successfully.");
} catch (err) {
console.error("β Error deleting folder:", err.message);
}
}
π§ͺ Step 10: Call Functions to Test
// Call functions one by one to test
// 1. Create folder
createFolder("myNotes");
// 2. Create file
// createFile("myNotes", "note.txt", "This is my first note!");
// 3. Read file
// readFile("myNotes", "note.txt");
// 4. Update file
// updateFile("myNotes", "note.txt", "Adding one more line");
// 5. Delete file
// deleteFile("myNotes", "note.txt");
// 6. Delete folder
// deleteFolder("myNotes");
π Uncomment one function at a time to test it step by step.
π§ What Students Will Learn
Feature | Concept Understood |
File operations | writeFile , readFile , appendFile , unlink , mkdir , rmdir |
Error handling | Using try...catch and error codes |
path module | Safe file paths in Node.js |
Project structure | Organizing real-world CLI projects |
β Sample Errors & How We Handled Them
Error Example | How We Handled |
Folder already exists | Checked err.code === 'EEXIST' |
Reading a non-existent file | catch block with custom message |
Trying to delete already deleted file | Proper try...catch to show clean error |
Writing to file in non-existent folder | Will throw error, we catch it |
πͺ Suggested Student Tasks
Add a function to rename a file
List all files in a folder using
fs.readdir()
Create a CLI interface using
readline
moduleAdd timestamps to file content on every update
β Summary
Task | Covered in this project |
Folder operations | Create and delete folders |
File operations | Create, read, update, delete files |
Error handling | Every function is wrapped in try...catch |
Clean code | Modular functions for each task |
π Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Node JS notes, tutorials, and project ideas β π© Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, π₯ Donβt forget to subscribe to my YouTube channel β Knowledge Factory 22 β for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. π
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: π In-depth explorations of emerging technologies π‘ Practical tutorials and how-to guides π§Insights on software development best practices πReviews of the latest tools and frameworks π‘ Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letβs connect and grow together! π