Node.js File System

Last Updated : 17 Jan, 2026

The fs (File System) module in Node.js is a built-in API used to perform file and directory operations on the server.

  • Provides file I/O operations using standard POSIX wrappers.
  • Supports both synchronous and asynchronous methods.
  • Imported using const fs = require('fs');.
  • Common operations include reading, writing, appending, closing, and deleting files.

Features of Node.js File System

  • Asynchronous and Synchronous Methods: Provides both non-blocking and blocking methods for various file operations.
  • Error Handling: Includes robust error handling to manage issues such as file not found or permission errors.
  • Directory Management: Allows creation, deletion, and listing of directories.

Synchronous Approach

Blocking functions run tasks sequentially, requiring one operation to finish completely before the next begins, which delays subsequent execution.

  • Follows a step-by-step execution flow.
  • Pauses program flow during operation execution.
  • Prevents parallel processing.
  • Subsequent tasks start only after completion.
  • May impact responsiveness in long-running operations.

Use Cases:

  • Suitable for lightweight tasks that execute quickly.
  • Simple to implement when performance impact is minimal.

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

GeeksforGeeks: A computer science portal

Create a main.js file with the following code: 

JavaScript
const fs = require("fs");

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

Output:

Synchronous read: GeeksforGeeks: A computer science portal

Asynchronous Approach

Non-blocking functions start operations without waiting for them to finish, enabling continuous execution while results are handled asynchronously.

  • Execution continues without waiting for operation completion.
  • Operations run in the background and return results when ready.
  • Enhances performance and application responsiveness.

Use cases:

  • Ideal for heavy or time-consuming tasks like large data processing.
  • Allows showing progress indicators while work continues in the background.
  • Best suited for GUI-based and responsive applications.

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

GeeksforGeeks: A computer science portal

Create a main.js file with the following code: 

JavaScript
const fs = require("fs");

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

Output:

Asynchronous read: GeeksforGeeks: A computer science portal

File Operations Using the fs Module in Node.js

The Node.js fs module provides methods to open, read, write, append, close, and delete files, enabling efficient file system operations.

Opening a File

Opening a file in Node.js allows you to create, read, or write files using the fs module.

  • fs.open() supports multiple file operations based on flags.
  • Can open files for reading, writing, or both.
  • Returns a file descriptor used for further file actions.

Example: Create a file named main.js having the following code to open a file input.txt for reading and writing. 

JavaScript
const fs = require("fs");

// Asynchronous - Opening File
console.log("opening file!");
fs.open("input.txt", "r+", function (err, fd) {
	if (err) {
		return console.error(err);
	}
	console.log("File open successfully");
});

Output:

opening file!
File open successfully

Syntax:

fs.open(path, flags, mode, callback)
  • path: File name or full file path.
  • flags: Defines open behavior (r, w, a, r+, etc.).
  • mode: File access permissions (default: read/write).
  • err: Error object if the operation fails.
  • data: File descriptor returned after successful open.

Reading a File

The fs.read() method is used to read data from an open file into memory.

  • Reads file content using the file descriptor (fd).
  • Stores the read data in a buffer for further processing.

Example: Create a file named main.js having the following code: 

JavaScript
const fs = require("fs");
const buf = new Buffer(1024);

console.log("opening an existing file");
fs.open("input.txt", "r+", function (err, fd) {
	if (err) {
		return console.error(err);
	}
	console.log("File opened successfully!");
	console.log("reading the file");

	fs.read(fd, buf, 0, buf.length, 0, function (err, bytes) {
		if (err) {
			console.log(err);
		}
		console.log(bytes + " bytes read");

		// Print only read bytes to avoid junk.
		if (bytes > 0) {
			console.log(buf.slice(0, bytes).toString());
		}
	});
});

Output:

opening an existing file
File opened successfully!
reading the file
40 bytes read
GeeksforGeeks: A computer science portal

Syntax:

fs.read(fd, buffer, offset, length, position, callback)
  • fd: This is the file descriptor returned by fs.open() method.
  • buffer: This is the buffer that the data will be written to.
  • offset: This is the offset in the buffer to start writing at.
  • length: This is an integer specifying the number of bytes to read.
  • Position: Specifies the starting point for reading if null, data is read from the current file position.
  • Callback: Executed after reading the file, receiving err for errors and data for the file contents.

Writing to a File

The fs.writeFile() method is used to asynchronously write data to a file in Node.js.

  • Overwrites the file if it already exists.
  • Writes data asynchronously for better performance.
  • Supports optional settings through the options parameter.

Example: Create a file named main.js having the following code: 

JavaScript
const fs = require("fs");

console.log("writing into existing file");
fs.writeFile("input.txt", "Geeks For Geeks", function (err) {
	if (err) {
		return console.error(err);
	}

	console.log("Data written successfully!");
	console.log("Let's read newly written data");

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

Output:

writing into existing file
Data written successfully!
Let's read newly written data
Asynchronous read: Geeks For Geeks

Syntax:

fs.writeFile(path, data, options, callback)
  • path: File location to write to, or a file descriptor.
  • data: Content to be written such as string or buffer.
  • options: Controls writing behavior like encoding, mode, and flag.
  • callback: Runs after the write operation completes.
  • err: Indicates an error if the write operation fails.

Appending to a File

Appending to a file adds new data at the end of an existing file without overwriting its content.

  • Uses fs.appendFile() to add data.
  • Preserves existing file content.
  • Writes data at the end of the file.

Example: Appending to a File Asynchronously.

javascript
const fs = require("fs");

let data = "\nLearn Node.js";

// Append data to file
fs.appendFile(
	"input.txt", data, "utf8",
	// Callback function
	function (err) {
		if (err) throw err;

		// If no error
		console.log("Data is appended to file successfully.");
	}
);

Output:

Data is appended to file successfully.

Example: Appending to a File Synchronously.

JavaScript
const fs = require("fs");

const data = "\nLearn Node.js";

// Append data to file
fs.appendFileSync("input.txt", data, "utf8");
console.log("Data is appended to file successfully.");

Output:

Data is appended to file successfully.
  • Before Appending Data to input.txt file:
GeeksforGeeks: A computer science portal 
  • After Appending Data to input.txt file:
GeeksforGeeks: A computer science portal
Learn Node.js

Syntax:

fs.appendFile(filepath, data, options, callback); 
// or
fs.appendFileSync(filepath, data, options);
  • filepath: It is a String that specifies the file path.
  • data: It is mandatory and it contains the data that you append to the file.
  • options: It is an optional parameter that specifies the encoding/mode/flag.
  • Callback: Function is mandatory and is called when appending data to file is completed.

Closing the File

The fs.close() method asynchronously closes an open file descriptor, releasing system resources and making the descriptor available for reuse.

  • Closes a file descriptor asynchronously.
  • Frees associated system resources.
  • Allows descriptor reuse for other files.
  • Should not be called during active file operations.

Example: Create a file named main.js having the following code: 

JavaScript
// Close the opened file.
fs.close(fd, function (err) {
	if (err) {
		console.log(err);
	}
	console.log("File closed successfully.");
});

Output:

File closed successfully.

Syntax:

fs.close(fd, callback)
  • fd: An integer representing the file descriptor to be closed.
  • callback: A function invoked after the close operation completes.
  • err: An error object returned if the operation fails.

Delete a File

The fs.unlink() method deletes a file or symbolic link from the filesystem.

  • Removes files or symbolic links.
  • Does not work on directories.
  • Use fs.rmdir() to delete directories.

Example: Create a file named main.js having the following code: 

JavaScript
const fs = require("fs");

console.log("deleting an existing file");
fs.unlink("input.txt", function (err) {
	if (err) {
		return console.error(err);
	}
	console.log("File deleted successfully!");
});

Output:

deleting an existing file
File deleted successfully!

Syntax:

fs.unlink(path, callback)
  • path: A string, Buffer, or URL representing the file or symbolic link to be removed.
  • callback: A function invoked after the removal operation completes.
  • err: An error object returned if the operation fails.

Advantages

  • Enables efficient reading, writing, and updating of files.
  • Supports a wide range of file system operations.
  • Provides detailed error messages to help with troubleshooting.

Also Check:

Comment

Explore