The most important functionalities provided by programming languages are Reading and Writing files from computers. Node.js provides the functionality to read and write files from the computer. Reading and Writing the file in Node.js is done by using one of the coolest Node.js modules called fs module, it is one of the most well-known built-in Node.js modules out there.
The file can be read and written in node.js in both Synchronous and Asynchronous ways. A Synchronous method is a code-blocking method which means the given method will block the execution of code until its execution is finished (i.e. Complete file is read or written). On the other hand, an Asynchronous method has a callback function that is executed on completion of the execution of the given method and thus allows code to run during the completion of its execution. Or according to modern JavaScript, asynchronous methods return a Promise which implies the eventual completion of the ongoing asynchronous method, the Promise will either be resolved or rejected. Thus, it is non-blocking.
Synchronous method to read the file: To read the file in the synchronous mode we use a method in the fs module which is readFileSync(). It takes two parameters first is the file name with a complete path and the second parameter is the character encoding which is generally 'utf-8'.
Example:
javascript
// Require the given module
const fs = require('fs');
// Use readFileSync() method
// Store the result (return value) of this
// method in a variable named readMe
// Keep the file in the same folder so
// donot need to specify the complete path
const readMe = fs.readFileSync('readMe.txt', 'utf-8');
// log the content of file stored in
// a variable to screen
console.log(readMe);
Output:Â

Synchronous method to writing into a file: To write the file in a synchronous mode we use a method in the fs module which is writeFileSync(). It takes two parameters first is the file name with the complete path in which content is to be written and the second parameter is the data to be written in the file.
javascript
// Require the given module
const fs = require('fs');
// Use readFileSync() method
// Store the result (return value) of this
// method in a variable named readMe
const readMe = fs.readFileSync('readMe.txt', 'utf-8');
// Store the content and read from
// readMe.txt to a file WriteMe.txt
fs.writeFileSync('writeMe.txt', readMe);
Asynchronous method to read and write from/into a file: To read/write the file in an asynchronous mode in the fs module we use the readFile() and writeFile() methods. The fs.readFile() takes three parameters first is the file name with the complete path, the second parameter takes the character encoding which is generally 'utf-8' and the third parameter is the callback function (which is fired after reading a complete file) with two parameters, one is the error in case error occurred while reading file and second is the data that we retrieve after reading the file and the fs.writeFile() also takes three parameters, file name with its complete path, the second parameter is the data to be written in file and the third is a callback function which fires in case an error occurs while writing the file.
Note: An Asynchronous method first completes the task (reading the file) and then fires the callback function.
javascript
// Require the given module
const fs = require('fs');
// Use readFile() method
fs.readFile('readMe.txt', 'utf-8', function(err, data) {
// Write the data read from readeMe.txt
// to a file writeMe.txt
if( !err )
fs.writeFile('writeMe.txt', data, (err)=>{
if( err ) {
throw err;
}
});
else
throw err;
});
Output:Â

If we talk in terms of Promise the asynchronous file handling method in Node.js, we have to use a promising method that takes a function following the callback approach as an input and returns a version of this function that returns a promise. The code looks like this as shown below (async-await approach has been followed here.) :
JavaScript
const {writeFile,readFile} = require('fs')
const {promisify} = require('util')
const readFileasync = promisify(readFile);
const writeFileasync = promisify(writeFile);
const file_handler = async()=>{
try {
const content = await writeFileasync('./writeMe.txt',"hello world");
try {
const data = await readFileasync('./writeMe.txt','utf-8');
console.log('New file has been created .');
console.log(data);
} catch (error) {
throw error;
}
} catch (error) {
throw error;
}
}
file_handler();
 Let's have a look at the terminal to figure out whether the code worked or not.

So, this is another way to achieve the same result as far as asynchronous file handling in Node.js is concerned.
Similar Reads
Node.js fs.filehandle.fd() Method The fs.filehandle.fd() method is an inbuilt application programming interface of class fs.filehandle within File System module which is used to provide the numeric file descriptor of this file handle object.Syntax:Â Â const filehandle.fd() Parameter: This method does not accept any parameter.Return v
3 min read
How to Copy a File in Node.js? Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
Node.js filehandle.read() Method The filehandle.read() method reads the file using a file descriptor. In order to read files without file descriptors the readFile() method of the filehandle package can be used. Node.js is used for server-side scripting. Reading and writing files are the two most important operations that are perfor
2 min read
Node.js fs.filehandle.sync() Method The fs.filehandle.sync() method is an inbuilt application programming interface of class fs.filehandle within File System module which is used to synchronize this file's in-core state with the storage device.Syntax:Â Â const filehandle.sync() Parameter: This method does not accept any argument as par
3 min read
Node.js fs.filehandle.write() Method The fs.filehandle.write() method is an inbuilt application programming interface of class fs.filehandle within the File System module which is used to write the data from the buffer to that particular file. Syntax:Â const filehandle.write(buffer[, offset[, length[, position]]]) Parameter: This metho
3 min read
Deploying Node.js Applications Deploying a NodeJS application can be a smooth process with the right tools and strategies. This article will guide you through the basics of deploying NodeJS applications.To show how to deploy a NodeJS app, we are first going to create a sample application for a better understanding of the process.
5 min read
Node.js filehandle.readFile() Method The filehandle.readFile() method is used to asynchronously read the file contents. This method reads the entire file into the buffer. It Asynchronously reads the entire contents of a file. Syntax: filehandle.readFile( options ) Parameters: The method accepts single parameter as mentioned above and d
2 min read
Import and Export in Node.js Importing and exporting files are important parts of any programming language. Importing functions or modules enhances the reusability of code. When the application grows in size, maintaining a single file with all the functions and logic becomes difficult. It also hinders the process of debugging.
3 min read
Node.js fs-extra ensureFile() Function The ensureFile() function makes sure that the file user is requesting exists. If the file doesn't exist the function will create a new file. Even if the user is requesting a file that is inside some directory, but if the directory does not exist the function will create the directory and the file in
3 min read
How to Install NPM FS in Node JS ? The file system module allows you to work with the file system on your computer NodeJS includes the fs module, to communicate with file systems. It provides functionality for interacting with the file system, such as reading from and writing to files, creating and removing directories, etc. The File
4 min read