The file system or fs module in node js is one of the core modules provided by Node.js, which provides us with the functionality to interact with and access the file system. This module helps us in reading, writing, and updating the file as well as other streams in node js. The fs module in node js is very helpful in performing all file Input / Output operations asynchronously as well as synchronously.
The file system or the fs module in node js supplies a lot of helpful functionality to access and interact with the file system. Being a part of the core modules of Node.js, the fs (file system ) module does not require installation from any external provider or through Node Package Manager (npm)
We can simply use all the methods and functionalities provided by the fs module just by requiring it.
Before ES6 syntax :
ES6 syntax :
The file system or the fs module in node js provides us with an Application Programming Interface (API) to interact with and access the File System of the operating system (Server).
Introduction to File System or fs Module in Nodejs
The file system or the fs module in node js enables us to interact and work with the file system, which is one of the fundamental tasks when writing a piece of code. To handle file operations like reading, updating and deleting, etc., Node.js provides us with the functionality of file Input and Output by providing wrappers around the standard POSIX Library functions. Each of those available methods in the fs module in node js is available in two versions, namely the (default) asynchronous version and the synchronous version.
Many developers think that working with a file is all about reading and writing to the file. In contrast, those two operations are the most commonly used; a lot more lies under the hood. The next topic will go into the details of those methods when working with the file system.
Usage for File System or the fs Module in Nodejs
Now we will discuss about the common usage of methods provided by the fs module in node js.
You can create a file with the fs.read() function or fs.readFile() to read files in an asynchronous manner or readFile Sync to read files in a synchronous manner.
Following is the syntax of fs.read() methods provided by the fs module in node js, to read from a specified file :
This method will use an (fd) file descriptor to read the file. In case, you want to read the file directly using the file name using other methods provided by the fs module in node js.
Description of the Parameters :
fd − It is the file descriptor returned by fs.open().
buffer − It is the buffer that the data will be written to.
offset − It is the offset in the buffer to start writing at.
length − It is an integer specifying the number of bytes to read.
position − It is used to specify the position where to begin reading the file. If the position is empty, data will be read from the current file position.
callback − It is the callback function that gets the three arguments (err, bytesRead, buffer).
Now run the index.js to see the result −
Method to read file asynchronously:-
Method to read file synchronously:-
There are three methods that can be used to create files and are provided by the fs module in node js.
fs.open() is used to create, open, and write files;
fs.appendFile() is used to create and populate a file;
fs.writeFile() is used to create and write files.
The program code below will create a file with the name mynewfile1.txt with the contents Hello Friends, Happy Coding! using the fs.appendFile().
Following is the syntax to create from a file using fs.open():−
Description of the Parameters :
path :<string> | <Buffer> | <URL> It is a String that specifies the file path.
flags :<string> | <number> See support of file system flags. Default: 'r'.
mode :<string> | <integer> Default: 0o666 (readable and writable)
callback : This function is to be executed when the file is opened.
err : This is the error that would be thrown if the method fails.
fd :<integer> It acts as a reference to the original file.
Now we will create a new file with the name mynewfile2.txt using fs.open().
In the above code example, the w (write) flag is used to tell the fs.open() function that you want to create or write to a new file.
This function will create an empty file if there is no file with the appointed name. Regardless, if the file already lives, the fs.open() function will override it.
The fs.writeFile(): is used to replace the file and its content if the file is found. If the file is not found, a new file will be created with the specified content.
The File System or the fs module in node js has these two methods for updating files:
The fs module in node js provides the fs.appendFile() is used to append the specified content at the end of the selected file.