Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Node.js File System

Node.js File System (FS)

Posted on October 5, 2025October 5, 2025 By Rishabh saini No Comments on Node.js File System (FS)

Node.js File System

In Node.js, file I/O operations are built upon simple wrappers around standard POSIX functions. The File System (fs) module in Node.js enables developers to work with files easily — from reading and writing to updating and deleting files.

Introduction to Applied AI:–Click Here

To use the File System module, you can import it using the following syntax:

var fs = require("fs");

Node.js FS – Reading a File

Every method in the fs module has both synchronous and asynchronous versions.

  • Asynchronous methods take a callback function as the last parameter and do not block the event loop, allowing other code to execute while the file operation is in progress.
  • Synchronous methods, on the other hand, block program execution until the operation completes.

The asynchronous approach is generally preferred for better performance and scalability.

Let’s look at an example.

Data Science Tutorial:-Click Here

Create a text file named input.txt with the following content:

File: input.txt

UpdateGadh is one of the best online tutorial websites to learn different technologies 
in a very easy and efficient manner.

Now, create a JavaScript file named main.js with the following code:

File: main.js

var fs = require("fs");  

// Asynchronous read  
fs.readFile('input.txt', function (err, data) {  
   if (err) {  
       return console.error(err);  
   }  
   console.log("Asynchronous read: " + data.toString());  
});  

// Synchronous read  
var data = fs.readFileSync('input.txt');  
console.log("Synchronous read: " + data.toString());  

console.log("Program Ended");  

Run the code in the Node.js terminal using the command:

Download New Real Time Projects :–Click here

node main.js

Node.js – Opening a File

You can open a file in asynchronous mode using the following syntax:

fs.open(path, flags[, mode], callback)

Parameters:

  • path: The name or path of the file to be opened.
  • flags: Specifies how the file should be opened (read, write, append, etc.).
  • mode: Defines the file permissions (default is 0666, which allows read and write).
  • callback: A function that receives two arguments — err and fd (file descriptor).

Machine Learning Tutorial:–Click Here

Common File Open Flags

FlagDescription
rOpen file for reading. Throws an error if the file doesn’t exist.
r+Open file for reading and writing. Throws an error if the file doesn’t exist.
rsOpen file for reading in synchronous mode.
rs+Open file for reading and writing in synchronous mode.
wOpen file for writing. Creates the file if it doesn’t exist, or truncates it if it does.
wxLike w but fails if the file already exists.
w+Open file for reading and writing. Creates or truncates as needed.
wx+Like w+ but fails if the file exists.
aOpen file for appending. Creates the file if it doesn’t exist.
axLike a but fails if the file exists.
a+Open file for reading and appending. Creates the file if it doesn’t exist.
ax+Like a+ but fails if the file exists.

Here’s an example of opening a file asynchronously:

Complete Advance AI topics:- CLICK HERE

File: main.js

var fs = require("fs");  

console.log("Going to open file!");  
fs.open('input.txt', 'r+', function(err, fd) {  
   if (err) {  
       return console.error(err);  
   }  
   console.log("File opened successfully!");       
});  

Run the program using:

node main.js

Node.js – File Information

You can get detailed information about a file using the fs.stat() method.

Syntax:

fs.stat(path, callback)

Parameters:

  • path: The name or path of the file.
  • callback: Function with two parameters — err and stats. The stats object is an instance of fs.Stats and provides various file properties and methods.

Deep Learning Tutorial:– Click Here

Common fs.Stats Methods

MethodDescription
stats.isFile()Returns true if the file is a regular file.
stats.isDirectory()Returns true if it’s a directory.
stats.isBlockDevice()Returns true if it’s a block device.
stats.isCharacterDevice()Returns true if it’s a character device.
stats.isSymbolicLink()Returns true if it’s a symbolic link.
stats.isFIFO()Returns true if it’s a FIFO.
stats.isSocket()Returns true if it’s a socket.

Example:

File: main.js

var fs = require("fs");  

console.log("Going to get file info!");  
fs.stat('input.txt', function (err, stats) {  
   if (err) {  
       return console.error(err);  
   }  
   console.log(stats);  
   console.log("Got file info successfully!");  
   console.log("isFile ? " + stats.isFile());  
   console.log("isDirectory ? " + stats.isDirectory());      
});  

Run the script using:

node main.js

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here


fs – npm path module in node js fs.writefile in node js read file in node js fs/promises node fs readfile node:fs/promises how to create a file in node js command prompt node js file system examples node js file system w3schools node js file system tutorial nodejs file system, nodejs file system module, nodejs file system tutorial, file system module nodejs, node.js file system, node js file system, node.js file system tutorial, node.js file system examples, file system node js, node js files system, node file system, node js file system module, nodejs filesystem, file system, nodejs file, file system module in node js, nodejs filesystem walk, nodejs filesystem module, file system module, read file system, filesystem nodejs module, nodejs file read write

    Post Views: 278
    Node.js Tutorial Tags:file system, file system module, file system module in node js, file system module nodejs, file system node js, filesystem nodejs module, node file system, node js file system, node js file system module, node js files system, node.js file system, node.js file system examples, node.js file system tutorial, nodejs file, nodejs file system, nodejs file system module, nodejs file system tutorial, nodejs filesystem, nodejs filesystem module, nodejs filesystem walk, read file system

    Post navigation

    Previous Post: Best Movie Review Sentiment Analysis Using Django
    Next Post: Best Vehicle Management System Using PHP

    More Related Articles

    Node.js MongoDB Filter Query Node.js MongoDB Filter Query Node.js Tutorial
    Node.js MongoDB Sorting Node.js MongoDB Sorting – Full Stack Web Project Node.js Tutorial
    Node.js Streams Node.js Streams Node.js Tutorial

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may also like

    1. Install Nodejs on Windows
    2. Node.js Command Line Options
    3. Node.js Assertion Testing
    4. Node.js Events
    5. Node.js MySQL Delete Records
    6. Node.js Create Connection with MongoDB

    Most Viewed Posts

    1. Top Large Language Models in 2025
    2. Online Shopping System using PHP, MySQL with Free Source Code
    3. login form in php and mysql , Step-by-Step with Free Source Code
    4. Flipkart Clone using PHP And MYSQL Free Source Code
    5. News Portal Project in PHP and MySql Free Source Code
    6. User Login & Registration System Using PHP and MySQL Free Code
    7. Top 10 Final Year Project Ideas in Python
    8. Online Bike Rental Management System Using PHP and MySQL
    9. E learning Website in php with Free source code
    10. E-Commerce Website Project in Java Servlets (JSP)
    • AI
    • ASP.NET
    • Blockchain
    • ChatCPT
    • code Snippets
    • Collage Projects
    • Data Science Project
    • Data Science Tutorial
    • DBMS Tutorial
    • Deep Learning Tutorial
    • Final Year Projects
    • Free Projects
    • How to
    • html
    • Interview Question
    • Java Notes
    • Java Project
    • Java Script Notes
    • JAVASCRIPT
    • Javascript Project
    • JSP JAVA(J2EE)
    • Machine Learning Project
    • Machine Learning Tutorial
    • MySQL Tutorial
    • Node.js Tutorial
    • PHP Project
    • Portfolio
    • Python
    • Python Interview Question
    • Python Projects
    • PythonFreeProject
    • React Free Project
    • React Projects
    • Spring boot
    • SQL Tutorial
    • TOP 10
    • Uncategorized
    • Online Examination System in PHP with Source Code
    • AI Chatbot for College and Hospital
    • Job Portal Web Application in PHP MySQL
    • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
    • Online Job Portal System in JSP Servlet MySQL

    Most Viewed Posts

    • Top Large Language Models in 2025 (8,613)
    • Online Shopping System using PHP, MySQL with Free Source Code (5,215)
    • login form in php and mysql , Step-by-Step with Free Source Code (4,867)

    Copyright © 2026 UpdateGadh.

    Powered by PressBook Green WordPress theme