JavaScript

File System (fs)

Read and write files with Node.js! Master the fs module for file operations.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Read and write files with Node.js! Master the fs module for file operations. This hands-on tutorial focuses on practical implementation of file system (fs) concepts.

File System (fs)

Unlike browser JavaScript, Node.js has direct access to your computer's hard drive. You can read, write, and delete files using the fs (File System) module.

1. Reading Files πŸ“–

There are two ways to do this:

  1. Asynchronous (Recommended): Non-blocking.
  2. Synchronous: Blocking (stops the program until done).
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

Note: If you don't specify "utf8", you get a Buffer (raw binary data) instead of a string.

2. Writing Files ✍️

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

3. The Modern Way: fs/promises ⚑

Callbacks can get messy. Node.js now has a Promise-based version of fs that works great with async/await.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

4. Working with Directories πŸ“

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Node.js fs module, synchronous vs asynchronous file operations, and fs/promises"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 4

Which module handles file operations?

file
fs
path
os

Key Takeaways

βœ… fs module reads/writes files.
βœ… Sync methods block code; Async methods don't.
βœ… Use fs/promises for clean async/await code.
βœ… fs.unlink deletes files.

What's Next?

We can read files... now let's build a tool that users can interact with from the terminal! Next up: Building CLI Tools.

Keep coding! πŸš€