The File System (fs) module allows Node.js to interact with files and directories on the system.
It is a core module, so no installation is required.
The fs module supports three styles of operations:
Executes operations sequentially
Blocks the event loop
Suitable for scripts and startup tasks
Example:
Non-blocking
Uses callbacks
Preferred for production apps
Example:
fs/promises)Uses Promises and async/await
Clean and modern approach
Example:
Streams allow reading/writing large files efficiently.
Always handle errors properly:
| Use Case | Recommended |
|---|---|
| Server apps | Async / Promises |
| Startup scripts | Sync |
| Large files | Streams |
Built-in and fast
Supports sync & async
Handles files and directories
Stream support for large data
fs is a core Node.js module
Supports synchronous, asynchronous, and promise-based APIs
Used for file and directory operations
Streams improve performance for large files
Take quizzes related to this topic and see where you stand!
Start Quiz Now