Introduction to Node.js File Systems and Streams
In Node.js, file handling is done using the <fs> module. It is one of the first practical skills you need when working on backend applications. Whether it’s uploading images, storing user data, or reading JSON files, you will use the file system in Node.js almost every time.
It allows you to read, write, update, and delete files easily. But what happens when files are very large? Reading at once can slow down or crash your server. That’s where node streams and node JS streaming comes into play. Streams help process data in small chunks instead of loading everything into memory.
In this blog, we will learn:
How does the node file system work?
How to create, write, and delete files?
What is a Node.js stream?
How does Node streaming improve performance?
File System in Node.js
The File System in Node.js is handled using the built-in <fs> module (File System Module). You don’t need to install everything separately; it comes with Node.js by default. It allows you to interact with files on your system, just like how app store images, logs, PDFs, or JSON data.
Here’s how you can import <fs> in Node.js:
That’s it. Now you can start working with files.
Example of Reading a File:
This reads the file asynchronously. Node.js does not block other operations while reading the file, which makes it powerful for backend development.
Example of Writing a File:
As a beginner, you must understand that:
<fs> works with both synchronous and asynchronous methods
Async methods are recommended for production
Used in almost every backend project
Essential for handling uploads, logs, reports, and configs
If you want to become confident in backend development, mastering the Node fs module is crucial.
Common Use Cases of the File System in Node.js
Understanding fs in Node.js becomes easier when you see where it’s actually used in real projects. Almost every backend application uses the Node.js file system in some form. Here are some practical use cases:
Uploading and Storing Files
When users upload profile pictures, resumes, or documents, the backend stores them using the fs node module.
Example:
Reading Configuration Files
Many projects store environment data in JSON files.
Logging System Data
Instead of printing everything to the console, production apps store logs in files.
Generating Reports (CSV/PDF/TXT)
Admin dashboards often generate downloadable reports. These are created and stored using the file system in Node.js.
Handling Large Media Files With Node Streams
If you try to read a 500MB video using normal file reading, your server may slow down. That’s why developers use Node streams and Node.js streaming to process files in chunks.
Creating and Writing Files
Creating and writing tasks are one of the most common tasks in the file system in Node.js. Whether you’re storing user data, logs, or generating reports, you’ll use the fs node js module regularly.
First, import the module:
Create & Write a New File
Use <fs.writeFile()> to create a file. If the file already exists, it will overwrite it.
This method is asynchronous, which means it won’t block your server.
Append Data to an Existing File
If you don’t want to overwrite the file, use <appendFile()>
Delete Files and Directories
You will also need to remove unused files while working with the file system in Node.js. The fs node js module provides simple methods for this.
Delete a File
Use <fs.unlink()> to remove a file.
This is asynchronous and recommended for production use.
Delete a Directory
To remove a folder:
If the folder contains files, use:
Always double-check file paths before deleting
Use <force: true> carefully
Prefer async methods in real apps
What are Streams in Node.js?
If you are working with large files in the node file system, reading everything at once is not a good idea. It consumes memory and can slow down your server. However, node streams simplify this process.
A node js stream is a way to handle data piece by piece instead of loading the entire file into memory.
If you use <fs.readFile()> on a 500MB file, Node.js loads the entire file into RAM. But with node streaming, data is processed gradually. This makes:
Video streaming faster
File downloads smoother
Large data processing efficient
Servers more scalable
Here is an example:
The file is read into chunks instead of one large block.
Types of Streams in Node.js
In Node.js streaming, there are four main types of Node streams. Understanding these is important before creating your own streams.
1. Readable Streams
Used to read data in chunks.
Example: Reading a large file using <fs.createReadStream()>
Data flows from the source to your application
2. Writable Streams
Used to write data in chunks
Example: Writing data to a file using <fs.createWriteStream>
Data flows from your application to the destination
3. Duplex Streams
Can read and write at the same time.
Example: Network sockets
4. Transform Streams
A special type of duplex stream that modifies data while passing it.
Example: Compressing files, encrypting data, and converting text formats
How to Create a Readable Stream in Node.js?
To create a Readable Stream in Node.js, we use <fs.createReadStream()>.
Example:
<createReadStream()> reads the file in chunks
<highWaterMarks> controls the chunk size
The <data> event fires every time a new chunk arrives
The <end> event runs when reading finishes
How to Create a Writable Stream in Node.js?
A writable stream allows you to write data in chunks instead of writing everything at once. This improves performance when handling large amounts of data. We create it using <fs.createWriteStream()> from the fs node module.
Example:
<createWriteStream()> creates a stream to write data
<write()> sends data in chunks
<end()> signals that writing is complete
<finish> event confirms success
Final Words
Understanding the file system is node.js is essential for any backend developer. The fs node js module allows you to create files, read data, update content, and delete files or directories. Almost every real backend project depends on the Node file system.
However, when performance becomes important, normal file reading methods are not enough. This is where node streaming is important. Instead of loading the entire file into memory, node streams process data in small chunks, making your application faster and memory-efficient.
Frequently Asked Questions (FAQs)
Q1. What is fs in Node.js?
Ans. <fs> in Node.js stands for the File System Module. It is a built-in module that allows you to work with files on your server. Using fs in Node.js, you can read, write, update, delete, and manage files and directories without installing any external packages.
Q2. How does the file system in Node.js work?
Ans. The file system in Node.js works through the <fs> module, which provides both synchronous and asynchronous methods. In production apps, asynchronous methods like <fs.readFile()> and <fs.writeFile()> are preferred because they do not block the event loop.
Q3. What are Node.js streams?
Ans. Node streams are a way to process data in chunks instead of loading everything into memory at once. This is especially useful for handling large files like videos, CSV files, or logs. Node.js streaming improves performance and reduces memory usage.
Q4. What is the difference between readFile and createReadStream?
Ans. <fs.readFile()> reads the entire file into memory before processing it. On the other hand, <fs.createReadStream()> reads the file in smaller chunks using a Node.js stream. For small files, <readFile> works fine. However, large files require node streaming.
Q5. Why should I use Node.js streaming?
Ans. You should see Node.js streams when handling large files, building file download APIs, streaming media content, processing large datasets, or transferring data between services. Streaming ensures your server remains stable even under heavy load.