Open In App

Node fs.open() Method

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

All the types of flags are described below:

Flag Description
r To open the file to read and throws 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.
w Open file for writing. A file is created if it doesn’t exist.
wx It 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.
a Open the file to append. A file is created if it doesn’t exist.
ax It 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:  Below examples illustrate the fs.open() method in Node.js:

Javascript




// Node.js program to demonstrate the
// fs.open() Method
 
// Include the fs module
const fs = require('fs');
 
// Open file demo.txt in read mode
fs.open('demo.txt', 'r', function (err, f) {
    console.log('Saved!');
});


The file is opened and the flag is set to read mode. After the opening of the file function is called to read the contents of the file and store them in memory. As there are no errors ‘saved‘ is printed.

Output:

Saved! 

Example 2:  Below examples illustrate the fs.open() method in Node.js:

Javascript




// Include the fs module
// Node.js program to demonstrate the
// fs.open() Method
 
// Include the fs module
const fs = require('fs');
 
console.log("Open file!");
 
// To open file in write and read mode,
// create file if doesn't exists.
fs.open('demo.txt', 'w+', function (err, f) {
    if (err) {
        return console.error(err);
    }
    console.log(f);
    console.log("File opened!!");
});


The file ‘demo.txt’ is opened in read and write mode, then the function is called. The output shows a number in the file when we print the value of ‘f’. 

Output: 

Open file!
10
File Opened!

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads