Synchronous File Operations
Understand when synchronous file APIs block the event loop and when they are acceptable.
Lesson content
π― Goal By the end of this lesson, you'll understand: What are Synchronous File Operations? Why do we use them? Common synchronous methods Advantages and disadvantages Real-world examples Interview questions What are Synchronous File Operations? Synchronous File Operations execute one operation at a time . Node.js waits until the current file operation is completed before executing the next line of code. Simple Definition: Synchronous operations block the execution until the task is finished. Why Do We Need Synchronous Operations? Sometimes we need to ensure that a file operation completes before the next step starts. Examples: Reading configuration files during application startup Running setup scripts Writing logs in small utility scripts Real-Life Example π Imagine borrowing a book from a library. Request Book β βΌ Wait Until Book Arrives β βΌ Read Book β βΌ Request Another Book You cannot move to the next step until the current one finishes. Import FS Module const fs = require ( "fs" ); 1. Read File Synchronously const fs = require ( "fs" ); const data = fs . readFileSync( "test.txt" , "utf8" ); console . log( data ); Output Hello Node.js 2. Write File Synchronously const fs = require ( "fs" ); fs . writeFileSync( "test.txt" , "Hello World" ); console . log( "File Created" ); Output File Created 3. Append File Synchronously const fs = require ( "fs" ); fs . appendFileSync( "test.txt" , " \n Welcome" ); console . log( "Data Added" ); Output Data Added 4. Rename File Synchronously const fs = require ( "fs" ); fs . renameSync( "test.txt" , "new.txt" ); console . log( "Renamed" ); 5. Delete File Synchronously const fs = require ( "fs" ); fs . unlinkSync( "new.txt" ); console . log( "Deleted" ); 6. Create Folder Synchronously const fs = require ( "fs" ); fs . mkdirSync( "Demo" ); console . log( "Folder Created" ); 7. Delete Folder Synchronously const fs = require ( "fs" ); fs . rmSync( "Demo" , { recursive: true , force: true }); console . log( "Folder Deleted" ); 8. Read Directory const fs = require ( "fs" ); const files = fs . readdirSync( "./" ); console . log( files ); Output [ "app.js", "package.json", "test.txt" ] 9. Get File Information const fs = require ( "fs" ); const stats = fs . statSync( "test.txt" ); console . log( stats . size); console . log( stats . isFile()); Output 1024 true How Synchronous Operations Work Application β βΌ readFileSync() β βΌ Wait... β βΌ Read Complete β βΌ Next Line Executes Synchronous vs Asynchronous Synchronous Asynchronous Blocks execution Doesn't block execution One task at a time Multiple tasks can continue readFileSync() readFile() Simpler for scripts Better for servers Example const fs = require ( "fs" ); console . log( "Start" ); const data = fs . readFileSync( "test.txt" , "utf8" ); console . log( data ); console . log( "End" ); Output Start Hello Node.js End The program waits until the file is fully read before printing "End" . Where Are Synchronous Operations Used? β Configuration file loading β Startup scripts β Build tools β CLI applications β Small utility scripts β Testing Advantages β Easy to understand β Sequential execution β Good for small scripts β No callbacks or Promises required Disadvantages β Blocks the Event Loop β Slower for web servers β Poor scalability β Not suitable for handling many concurrent users Interview Question What is the output? const fs = require ( "fs" ); console . log( "Start" ); fs . writeFileSync( "test.txt" , "Hello" ); console . log( "End" ); Output Start End Reason: writeFileSync() completes first, then the next line executes. Best Practices β Use synchronous methods during application startup or in CLI tools. β Use asynchronous methods in production web servers. β Always use try...catch to handle errors. try { const data = fs . readFileSync( "test.txt" , "utf8" ); console . log( data ); } catch ( err ) { console . error( err . message); } β Avoid synchronous file operations inside API request handlers because they block the Event Loop. Key Points β Synchronous file operations block execution until completion. β They are simple and execute code sequentially. β Common methods include readFileSync() , writeFileSync() , appendFileSync() , renameSync() , unlinkSync() , mkdirSync() , rmSync() , readdirSync() , and statSync() . β Best suited for startup tasks, scripts, and CLI applications. β Avoid them in high-performance Node.js servers. π§ Easy Formula Need File Operation β βΌ Sync Method β βΌ Wait Until Complete β βΌ Execute Next Line readFileSync() β Read writeFileSync() β Create/Overwrite appendFileSync() β Add Data renameSync() β Rename unlinkSync() β Delete File mkdirSync() β Create Folder rmSync() β Delete Folder readdirSync() β Read Directory statSync() β File Information Remember: β³ Synchronous = Wait until the operation finishes. π Simple but blocks the Event Loop. π Best for startup scripts, CLI tools, and configuration loadingβnot for production request handling.