Node.js Course
Node.js
/
Beginner

File System (fs) Module

Definition

A core Node.js module used to interact with the hard drive. It provides functions to create, read, update, delete, and rename files and directories.

Explain Like I'm New

It's your application's hands. It allows Node to reach out of the code, grab a text file off your desktop, read what's inside, write a new line to it, and save it. It has three versions of its tools: Callbacks (old), Synchronous (blocking), and Promises (modern).

Real World Example

Writing a script that opens a massive `.csv` file full of user data, converts it to JSON, and saves the new JSON file back to the hard drive.

Common Use Cases

  • •Reading config files
  • •Saving user uploads
  • •Generating dynamic logs

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How do you import the promise-based version of the fs module?

intermediate

  • Why should you avoid using `fs.readFileSync()` in a web server route?

advanced

  • How do you read a file that is too large to fit in RAM?

Flash Cards

Question

How to import the promise version?

Click to reveal answer
Answer

`const fs = require('fs/promises');` or `import fs from 'fs/promises';`.

Question

Why avoid readFileSync in servers?

Click to reveal answer
Answer

Because it is Synchronous! If 1,000 users hit your server, and the server does `readFileSync`, the entire Node.js application completely freezes and stops responding to EVERYONE until that file is finished reading. Always use the async/promise versions.