What is the fs module?
The fs module allows us to deal with file-related operations in Nodejs. This built-in module can perform various file actions such as read, write, add, delete, and so on. It is responsible for all forms of synchronous and asynchronous I/O-related file operations, and in Nodejs, we choose the Asynchronous implementation because it never interrupts the program execution, even in the case of bulk requests.
Getting Started with the Implementation
As the fs module is a core module in Nodejs, it does not require installation from a third party or through the npm (node package manager). To use the fs module's features and methods in the application, we must import it using the syntax below.
Let's have a look at some of the most regularly utilized fs module functions. The asynchronous Method is used to implement the following functions.
- writeFile
- appendFile
- readFile
- unlink
writeFile
This function call works in two different ways.
- If a file with the supplied name and location exists, the content of the file will be replaced.
- If a file with the specified name and location does not exist, this method will create one and add the content to it.
Explanation of the function parameters listed above:-
i) filename:- This field contains
the filename (for example, "input.txt"). You can pass the location of the file in the filename parameter also.
ii) content: This field contains information that should be written in a file
(for example, "Hello World").
iii) callback function:- This function call will be executed after the
writeFile() method, in both failure and success cases.
Example for the writeFile Function:-
Create a file and write the below code and save the file with .js extension.
The above-created js file is executed in either the standard
command prompt (cmd) or the nodejs cmd, yielding the following results:-
The "input.txt" file is generated in the same directory where I saved the fileConcept.js, and the content is saved in it.
appendFile
This function call works in two different ways.
- If a file with the supplied name and location exists, the content of the file will be appended with the new content.
- If a file with the specified name and location does not exist, this method will create one and add the content to it.
Example for the appendFile Function:-
Create a file and write the below code and save the file with .js extension.
After executing the above file in cmd, this is the response saved in the “input.txt” file
readFile
This function call reads the content from a file.
Example for the readFile Function:-
Create a file and write the below code and save the file with .js extension.
Execute the above file, to see the content in the console
unlink
This function is used to delete a file based on the file name or the location specified in the system.
Create a file and write the below code and save the file with the .js extension.
Execute the above file, to see the content in the console
The program is created and executed practically for some of the most regularly used functions of the fs module. While learning, you can utilize the given code structure and try to implement it on your own to have a better knowledge of the subject. In the next blogs, we will attempt to implement some of the functions using the fs module for a better understanding.