In Node.js, file I/O is abstracted through a wrapper around standard POSIX functions. The Node File System (fs) module can be imported using the following syntax:
var fs = require('fs')
Exploring Node.js File System (FS)
Reading files with FS in Node.js
Each method in the fs module has synchronous and asynchronous patterns. Asynchronous methods take the last parameter as a callback function. Asynchronous methods are prioritized as they never block program execution, unlike synchronous methods.
To illustrate, consider the following example for a better understanding:
Create a text file named input.txt with the following content:
Javatpoint is a premier online tutorial platform to master various technologies
in a user-friendly and efficient manner.
Alternatively, consider creating a JavaScript file named main.js containing the code snippet below:
import fs from 'fs';
// Reading file asynchronously
fs.promises.readFile('input.txt').then((data) => {
if (!err) {
if (err) return console.error(err);
}
console.log('Read operation completed asynchronously: ' + data.toString());
});
// Synchronous file read operation
var data = fs.readFileSync('input.txt');
console.log('Read file synchronously: ' + data.toString());
console.log('Program execution completed');
Open Node.js command prompt and run main.js:
node main.js
File Handling in Node.js
Below is the syntax to open a file in asynchronous mode:
fs.open(path, flags[, mode], callback)
In the above syntax:
- Path: This string represents the file name and path to the file.
- Flag: Flag represents the behavior of the opened file. All possible values are listed in the table below.
- Mode: Set the file mode; these modes only apply when the file is created. The default value is 0666, readable and writeable.
- Callback: The callback function takes 2 arguments (err, fd).
Flag for Reading/Writing in Node.js
Below is a table listing flags for reading/writing in Node.js:
Example:
Create a JavaScript file named main.js containing the code below to open the file input.txt for reading and writing:
var fs = require('fs');
// Asynchronous - Opening File
console.log('Attempting to open the file!');
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log('File opened successfully!');
});
Open Node.js command prompt and run main.js:
Method to Retrieve File Information in Node.js
Use the syntax below to get file information in Node.js:
fs.stat(path, callback)
Where:
- Path: This is a string representing the file name, including the path.
- Callback: Callback function to receive 2 arguments (err, stats), where the stats argument is an object of .Stats.
Class fs.Stats in Node.js
For a clearer understanding, refer to the example below. First, create a JavaScript file named main.js, containing the code below:
var fs = require('fs');
console.log('Attempting to retrieve file information!');
fs.stat('input.txt', function (err, stats) {
if (err) {
return console.error(err);
}
console.log(stats);
console.log('File information retrieved successfully!');
// Verify file type
console.log('Is it a file? ' + stats.isFile());
console.log('Is it a directory? ' + stats.isDirectory());
});
Open Node.js command prompt and run main.js:
node main.js
Thus, the above article from Mytour provides an introduction to File System (FS) in Node.js. Additionally, readers can explore more articles under Learn Node.js on Mytour to delve deeper into concepts such as Buffer or Stream in Node.js.