Open In App

Node fs.open() Method

Last Updated : 19 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To create the file, to write to a file, or to read a file fs.open() method is used. fs.readFile() is only for reading the file and similarly fs.writeFile() is only for writing to a file, whereas fs.open() method does several operations on a file. First, we need to load the fs class which is a module to access the physical file system. For it require method is used. For example: var fs = require('fs'); 

Syntax: 

fs.open( filename, flags, mode, callback )

Parameter: This method accepts four parameters as mentioned above and described below:  

  • filename: It holds the name of the file to read or the entire path if stored at another location.
  • flag: The operation in which the file has to be opened.
  • mode: Sets the mode of the file i.e. r-read, w-write, r+ -readwrite. It sets to default as readwrite.
  • callback: It is a callback function that is called after reading a file. It takes two parameters: 
    • err: If any error occurs.
    • data: A file descriptor, used by subsequent file operations. A file descriptor is a handle used to access a file. It is a non-negative integer uniquely referencing a specific file.

The fs.open() method is used to open files in Node.js.

All the types of flags are described below:

FlagDescription
rTo open the file to read and throw an exception if the file doesn't exist.
r+Open the file to read and write. Throws an exception if the file doesn't exist.
rs+Open files in synchronous mode to read and write.
wOpen file for writing. A file is created if it doesn't exist.
wxIt is the same as 'w' but fails if the path exists.
w+Open the file to read and write. A file is created if it doesn't exist.
wx+It is the same as 'w+' but fails if the path exists.
aOpen the file to append. A file is created if it doesn't exist.
axIt is the same as a but fails if the path exists.
a+Open the file for reading and appending. A file is created if it doesn't exist.
ax+It is the same as 'a+' but fails if the path exists.

Example 1: Opening a File in Read Mode (r)

JavaScript
// Include the fs module
const fs = require('fs');

// Open file demo.txt in read mode
fs.open('demo.txt', 'r', function (err, fd) {
    if (err) {
        return console.error("Error opening file:", err);
    }
    console.log("File Descriptor:", fd);
});
  • Opens the file demo.txt in read-only mode.
  • Prints the file descriptor (e.g., 10), which is used for further file operations.

Output

File Descriptor: 3

Example 2: Opening a File in Read & Write Mode (w+)

JavaScript
// Include the fs module
const fs = require('fs');

console.log("Opening file...");

// Open file in write and read mode
fs.open('demo.txt', 'w+', function (err, fd) {
    if (err) {
        return console.error("Error opening file:", err);
    }
    console.log("File Descriptor:", fd);
});
  • Opens the file demo.txt in read and write mode (creates the file if it doesn't exist).
  • Prints the file descriptor (e.g., 12), which can be used for reading and writing the file.

Output

Opening file...
File Descriptor: 3

We have a Cheat Sheet on Nodejs fs modules where we covered all the fs methods to check those please go through Node File System Module Complete Reference this article.


Similar Reads