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: 

const fs = require('fs');


That’s it. Now you can start working with files. 


Example of Reading a File: 

const fs = require('fs');


fs.readFile('example.txt', 'utf8', (err, data) => {

  if (err) {

    console.error(err);

    return;

  }

  console.log(data);

});


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: 

const fs = require('fs');


fs.writeFile('example.txt', 'Hello Node.js!', (err) => {

  if (err) throw err;

  console.log('File created successfully!');

});


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: 

fs.writeFile('uploads/profile.png', fileData, (err) => {

  if (err) throw err;

  console.log('File uploaded successfully');

});

Reading Configuration Files

Many projects store environment data in JSON files. 

fs.readFile('config.json', 'utf8', (err, data) => {

  const config = JSON.parse(data);

  console.log(config);

});

Logging System Data

Instead of printing everything to the console, production apps store logs in files. 

fs.appendFile('logs.txt', 'User logged in\n', (err) => {

  if (err) throw err;

});

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: 

const fs = require('fs');

Create & Write a New File

Use <fs.writeFile()> to create a file. If the file already exists, it will overwrite it. 

fs.writeFile('data.txt', 'Welcome to Node.js File System!', (err) => {

  if (err) throw err;

  console.log('File created successfully');

});


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()>

fs.appendFile('data.txt', '\nNew line added.', (err) => {

  if (err) throw err;

  console.log('Content appended');

});

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. 

const fs = require('fs');


fs.unlink('data.txt', (err) => {

  if (err) throw err;

  console.log('File deleted successfully');

});


This is asynchronous and recommended for production use. 

Delete a Directory

To remove a folder: 

fs.rmdir('oldFolder', (err) => {

  if (err) throw err;

  console.log('Folder deleted');

});


If the folder contains files, use: 

fs.rm('oldFolder', { recursive: true, force: true }, (err) => {

  if (err) throw err;

  console.log('Folder removed completely');

});


  • 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: 

const fs = require('fs');


const readStream = fs.createReadStream('bigfile.txt', 'utf8');


readStream.on('data', (chunk) => {

  console.log('New chunk received');

});


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: 

const fs = require('fs');


const readStream = fs.createReadStream('largefile.txt', {

  encoding: 'utf8',

  highWaterMark: 1024 // reads 1KB at a time

});


readStream.on('data', (chunk) => {

  console.log('Chunk received:');

  console.log(chunk);

});


readStream.on('end', () => {

  console.log('File reading completed');

});


readStream.on('error', (err) => {

  console.error('Error:', err);

});


  • <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: 

const fs = require('fs');


const writeStream = fs.createWriteStream('output.txt');


writeStream.write('Hello ');

writeStream.write('from ');

writeStream.write('Node.js Streams!\n');


writeStream.end();


writeStream.on('finish', () => {

  console.log('Writing completed');

});


writeStream.on('error', (err) => {

  console.error('Error:', err);

});


  • <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.

Discover More Courses on Skillwaala