Node.js File System

Node js 10 min min read Updated: Mar 29, 2026 Beginner
Node.js File System
Beginner Topic 6 of 12

Node.js File System (fs Module)

The File System module, commonly known as fs, is one of the most important core modules in Node.js. It allows developers to interact with the file system of the operating system. Using this module, you can create, read, update, delete, and manage files and directories.

Whether you are building a backend API, handling logs, uploading files, or working with data storage, the fs module plays a crucial role in real-world Node.js applications.

Key Concept: The fs module allows you to perform file operations like reading, writing, updating, and deleting files.

What is the fs Module?

The fs module is a built-in (core) module in Node.js, so you do not need to install it separately. It provides both synchronous and asynchronous methods to work with files.

To use the fs module, you first need to import it using require():

javascript const fs = require("fs");

Reading Files in Node.js

One of the most common tasks is reading the content of a file. Node.js provides the fs.readFile() method for this purpose. This method reads the file asynchronously, which means it does not block other operations while reading the file.

javascript fs.readFile("file.txt","utf8",(err,data)=>{ console.log(data); });

In this example:

  • "file.txt" is the file name
  • "utf8" specifies the encoding format
  • err handles errors if the file is not found
  • data contains the file content
Output

Content of the file will be displayed here

Handling Errors Properly

It is always a good practice to handle errors when working with file operations. For example:

javascript fs.readFile("file.txt","utf8",(err,data)=>{ if(err){ console.error("Error reading file:", err); return; } console.log(data); });

This ensures that your application does not crash if something goes wrong, such as the file not existing.

Synchronous vs Asynchronous Methods

Asynchronous (Recommended)

Asynchronous methods do not block the execution of the program. Node.js continues executing other tasks while the file operation is in progress.

Synchronous

Synchronous methods block the execution until the operation is complete. These are simpler but not recommended for production applications.

javascript const data = fs.readFileSync("file.txt","utf8"); console.log(data);

Writing Files

You can create or overwrite a file using fs.writeFile():

javascript fs.writeFile("file.txt","Hello Node.js",(err)=>{ if(err) throw err; console.log("File written successfully"); });

Appending Data to Files

To add content to an existing file without overwriting it, use fs.appendFile():

javascript fs.appendFile("file.txt","\nNew content added",(err)=>{ if(err) throw err; console.log("Content appended"); });

Deleting Files

You can delete a file using fs.unlink():

javascript fs.unlink("file.txt",(err)=>{ if(err) throw err; console.log("File deleted"); });

Working with Directories

Create Directory

javascript fs.mkdir("myFolder",(err)=>{ if(err) throw err; console.log("Folder created"); });

Read Directory

javascript fs.readdir("./",(err,files)=>{ console.log(files); });

Remove Directory

javascript fs.rmdir("myFolder",(err)=>{ if(err) throw err; console.log("Folder removed"); });

Real-World Use Cases

  • Reading configuration files
  • Handling file uploads
  • Logging application data
  • Generating reports
  • Managing static files

Best Practices

  • Always use asynchronous methods for better performance
  • Handle errors properly to avoid crashes
  • Use streams for large files instead of reading everything at once
  • Validate file paths to prevent security issues

Common Mistakes

  • Not handling errors in callbacks
  • Using synchronous methods in production
  • Incorrect file paths
  • Forgetting encoding (utf8)

Conclusion

The fs module is a powerful tool in Node.js that allows developers to work with files and directories efficiently. From reading and writing files to managing directories, it is widely used in almost every backend application.

By understanding how to use the File System module, you can build applications that interact with data stored on your system and handle real-world file operations effectively.

Quick Summary: The fs module helps you perform file operations like reading, writing, updating, and deleting files in Node.js.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators