File System Operations

Read, write, update, and remove files with the Node.js file-system APIs.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is the File System (FS) module? Why do we use it? Common File System Operations Synchronous vs Asynchronous methods Real-world examples Interview questions What is the File System (FS) Module? The File System ( fs ) module is a built-in Node.js module used to work with files and directories. It allows you to: Read files Write files Create files Delete files Rename files Create folders Delete folders Simple Definition: The FS module helps Node.js interact with the computer's file system. Import FS Module const fs = require ( "fs" ); Real-Life Example 📁 Imagine your computer is a library. Library │ ├── Read Book ├── Write Notes ├── Add New Book ├── Rename Book └── Remove Book Similarly, the FS module manages files and folders. 1. Read File Asynchronous const fs = require ( "fs" ); fs . readFile( "test.txt" , "utf8" , ( err , data ) => { if ( err ) throw err ; console . log( data ); }); Output Hello Node.js Synchronous const fs = require ( "fs" ); const data = fs . readFileSync( "test.txt" , "utf8" ); console . log( data ); 2. Write File Creates a new file or overwrites an existing file. const fs = require ( "fs" ); fs . writeFile( "test.txt" , "Hello World" , ( err ) => { if ( err ) throw err ; console . log( "File Created" ); }); Output File Created 3. Append File Adds data to an existing file. const fs = require ( "fs" ); fs . appendFile( "test.txt" , " \n Welcome" , ( err ) => { if ( err ) throw err ; console . log( "Data Added" ); }); 4. Rename File const fs = require ( "fs" ); fs . rename( "test.txt" , "new.txt" , ( err ) => { if ( err ) throw err ; console . log( "Renamed" ); }); 5. Delete File const fs = require ( "fs" ); fs . unlink( "new.txt" , ( err ) => { if ( err ) throw err ; console . log( "Deleted" ); }); 6. Create Folder const fs = require ( "fs" ); fs . mkdir( "Demo" , ( err ) => { if ( err ) throw err ; console . log( "Folder Created" ); }); 7. Delete Folder const fs = require ( "fs" ); fs . rmdir( "Demo" , ( err ) => { if ( err ) throw err ; console . log( "Folder Deleted" ); }); Note: fs.rmdir() is deprecated for removing non-empty directories. Use: fs . rm( "Demo" , { recursive: true , force: true }, ( err ) => { if ( err ) throw err ; console . log( "Folder Deleted" ); }); 8. Check if File Exists const fs = require ( "fs" ); fs . access( "test.txt" , ( err ) => { if ( err ) { console . log( "File Not Found" ); } else { console . log( "File Exists" ); } }); 9. Get File Information const fs = require ( "fs" ); fs . stat( "test.txt" , ( err , stats ) => { if ( err ) throw err ; console . log( stats . size); console . log( stats . isFile()); }); Output 1024 true Synchronous vs Asynchronous Synchronous Asynchronous Blocks execution Doesn't block execution readFileSync() readFile() writeFileSync() writeFile() Slower for servers Better for servers File System Workflow Application │ ▼ FS Module │ ▼ Operating System │ ▼ Hard Disk │ ▼ Result Common File System Methods Method Purpose readFile() Read file writeFile() Create/Overwrite file appendFile() Add data rename() Rename file unlink() Delete file mkdir() Create folder rm() Delete file/folder stat() File information access() Check file accessibility Real-Life Example 📚 School Records Student File │ ▼ Read Details │ ▼ Update Marks │ ▼ Save File │ ▼ Rename File Where is the FS Module Used? ✅ File Upload Systems ✅ Log Files ✅ Report Generation ✅ Configuration Files ✅ Image Storage ✅ Backup Systems Interview Question Which method appends data to a file? Answer fs.appendFile() Which method returns file information? Answer fs.stat() Best Practices ✅ Prefer asynchronous methods in server applications. ✅ Always handle errors. ✅ Use Streams for very large files instead of readFile() . ❌ Avoid synchronous methods in production request handlers because they block the Event Loop. Key Points ✅ The FS module is built into Node.js. ✅ It provides APIs to manage files and directories. ✅ Asynchronous methods are preferred for scalable applications. ✅ readFile() , writeFile() , appendFile() , rename() , unlink() , mkdir() , rm() , stat() , and access() are commonly used. ✅ Streams are better than readFile() for processing large files. 🧠 Easy Formula Need File Operations? │ ▼ FS Module │ ┌──────┼──────────┐ ▼ ▼ ▼ Read Write Delete │ │ │ ▼ ▼ ▼ File Update Remove readFile() → Read writeFile() → Create/Overwrite appendFile() → Add Data rename() → Rename unlink() → Delete File mkdir() → Create Folder rm() → Delete Folder/File stat() → File Info access() → Check Access Remember: 📂 fs = File System module 📖 readFile() = Read file ✍️ writeFile() = Create or overwrite file ➕ appendFile() = Add data 🗑️ unlink() = Delete file 📁 mkdir() = Create folder 🚀 Prefer asynchronous FS methods for better performance in Node.js servers.