Updated April 6, 2023
What is Node.js File System?
Every Programming language has the file system. Node.js also has its own module file system to work with files related operations. The file system in node.js enables a simple API with which we can work with files. We can work with file systems in both ways Synchronously and Asynchronously. If you are familiar with synchronous and asynchronous ways of programming then you can get it more easily. Every time we import other modules as required in node.js we have to import the file system also. The FS makes very simple execution of working with files in node.js.
How to Open File in Node.js?
For using any module in node.js we need to install it. The file system module is available in the npm (node package manager) package. Just run below command in your command prompt and it will get installed on your machine.
npm install fs --save
Syntax for Importing fs module in node.js:
var fs = require("fs");
There are several operations in the FS module with which we can work. Let’s start with the first operation in this module. Mostly we are using the Asynchronous way of opening a file.
The open() methods used to open a file. This method has several parameters like file name, mode of operation to be performed on that file and since we are looking for asynchronous code we will give a callback function which will throw an error if something went wrong.
Syntax:
fs.open(path, mode of operation, callback function);
Example:
var fileSystem = require("fs");
var dir = "c:\\Users\\Public\\Two.txt";
fileSystem .open(dir, "w+", function(error, data) {
if (error) {
console.error("The error is " + error.message);
} else {
console.log("The respective file is opened at " + dir);
}
});
Output:
How to Read File?
Before going forward with the all file operations lets first understand what is meant by synchronous and asynchronous way of writing methods in node.js. This is so important to understand why we are considering these two different ways of writing the code. Synchronous way of writing code means it will execute your code step by step. After completing one statement it will go for another.
On the other side, if you don’t want to wait for the further statement to execute, or there is a delay in executing any function then you can call the method asynchronously. This helps us to achieve better performance as compared to an asynchronous way of writing code. If you still wonder how it exactly works then you need to check JavaScript ES5, and the above version to know more. The Next operation in the file system is to read the file in node.js. Reading the file has a method called readFile(). This method has three parameters. First is the name of the file to read. The second parameter is to know in which mode we have to read the given file. And the third one is the callback function. This callback function used to throw an error if any.
Syntax:
fs.readFile(Name of the file, [mode of the file], callback function)
Example:
var fileSystem = require('fs');
fileSystem.readFile('two.txt', 'utf-8',function (error, data){
if (error) throw error;
console.log(data);
});
Save the above file with the same name. we are going to read the two.txt file which we created already.
Output:
readfile simply reads the content in the file which we have in two.txt. Now we will see the synchronous way of reading file. Here we are not giving callback function.
Example:
var fileSystem = require('fs');
var fileContent = fileSystem.readFileSync('two.txt', 'utf-8');
console.log(fileContent);
Output:
How to Write File in Node.js?
Here, we are using an asynchronous way of writing the code.
Example:
var fileSystem = require("fs");
console.log("This example to open file in write mode");
fileSystem.open('two.txt', 'w+', function(error, callback) {
if (error) {
return console.error(error);
}
console.log("File has been opened in write mode successfully!");
});
Output:
If you observed the above program carefully, we are writing to the file. And W+ is the parameter we are passing to tell the compiler that to write the content to the file. If the file does not exist then create the new file and write the content into that. So here we have created two.txt file and wrote the content successfully one that.
Various Flags of File System in Node.js
Various Flags of File System in Node.js are given below:
- a: We are using this flag to add data to the file. If the file is not created then it creates that file and adds specified data.
- ax: Same like a flag but it will work a single time only.
- a+: Open file for reading and appending. The file is created if it does not exist.
- ax+: We can append and read like ‘a+’ but we can open it exclusively only.
- w: This flag used to open a file in write mode. The file is created if the existing file is not created.
- wx: We are opening this file exclusively for writing. It makes sure the file is newly created.
- w+: It is used for reading and writing. The one thing it creates the file if not exist. And delete the file if it already exists.
- wx+: It works like ‘w+’ but fails if the file already exists.
- r+: Open file for reading and writing. if the given file does not exist then It throws an error.
- rs: This flag is used to open the file in reading with synchronous mode.
- r: This flag is to open the file for reading purposes. It fetches error if the file does not exist.
- rs+: This flag is used for both reading and writing purposes. This works for synchronous operation.
Recommended Articles
This is a guide to Node.js File System. Here we also discuss the Introduction and syntax along with how to open a file in node.js?. You may also have a look at the following articles to learn more –