πŸ“ Day 10: Mini Project β€” Create a Local File Manager in Node.js

Payal PorwalPayal Porwal
4 min read

🎯 Project Goal

Create a CLI-based (Command Line Interface) File Manager that allows users to:

  1. Create a folder

  2. Create a file inside that folder

  3. Read the file

  4. Update the file

  5. Delete the file or folder

All operations must include proper error handling.


πŸ“¦ Tools Used

  • Node.js

  • fs/promises module

  • path module

  • Basic knowledge of async/await and try...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 folder

  • path.join() ensures correct folder path

  • Errors 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

FeatureConcept Understood
File operationswriteFile, readFile, appendFile, unlink, mkdir, rmdir
Error handlingUsing try...catch and error codes
path moduleSafe file paths in Node.js
Project structureOrganizing real-world CLI projects

❓ Sample Errors & How We Handled Them

Error ExampleHow We Handled
Folder already existsChecked err.code === 'EEXIST'
Reading a non-existent filecatch block with custom message
Trying to delete already deleted fileProper try...catch to show clean error
Writing to file in non-existent folderWill throw error, we catch it

πŸ’ͺ Suggested Student Tasks

  1. Add a function to rename a file

  2. List all files in a folder using fs.readdir()

  3. Create a CLI interface using readline module

  4. Add timestamps to file content on every update


βœ… Summary

TaskCovered in this project
Folder operationsCreate and delete folders
File operationsCreate, read, update, delete files
Error handlingEvery function is wrapped in try...catch
Clean codeModular 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. πŸš€

0
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! 🌟