Blog Blog Posts Business Management Process Analysis

File System Module in Node.js

The main objective of this module is to focus on the operations offered by Node.js through the File System.

If you have missed out on the basics of NodeJS, refer to What is Node.js? Use Cases, Architecture & Advantages

What do you mean by File System?

To manage file operations like creating, accessing, erasing, and other similar tasks, Node.js provides a built-in module called FS (File System).

Node.js provides interfaces, mostly around common POSIX functions, to provide file I/O capabilities. 

Depending on a user’s needs, all file system processes can be either asynchronous or synchronous.

It is possible to interact with your computer’s file system thanks to the Node.js file system module.

Use the require() function to incorporate the File System module:

var fs = require('fs');

Now that you have understood what is meant by File Systems, let us understand:

What is A File System Module used for?

The File System module is frequently used for the following purposes:

Let’s discuss each of them in-depth!!

Using the fs.readFile() method and providing the file path, encoder, and a callback function which will be invoked with the data files (and any errors) is the easiest way to read a file in Node.js.

Files can be read in Node in three different methods. 

The promise way, the callback way, and the synchronous way are the names of these approaches.

The Top 50 Node.js Interview Questions and Answers for 2022 : A great resource for brushing up on your knowledge before upcoming interviews.

There are several ways to create new files in the File System module:


To asynchronously add the supplied data to a file, use the fs.appendFile() method. If the file doesn’t already exist, one is created. You can alter how the operation behaves by using the options parameter.

The syntax goes as follows:

fs.appendFile( path, data[, options], callback )

Multiple operations are performed on a file using the function.

The fs class, a module that allows access to the physical file system, must first be loaded. A required procedure is employed for it.

 For instance: var fs = require(‘fs’)

Syntax: filename, flags, mode, callback )

The asynchronous writing of the provided data to a file is accomplished using the fs.writeFile() method. If the file already exists, it will usually be replaced. You can alter the method’s functionality by using the ‘options’ parameter.

The syntax for the same is as follows:

fs.writeFile( file, data, options, callback )

Updates to files can be made using the File System module’s methods:


The selected file and its contents are replaced via the fs.writeFile() method:

var fs = require('fs');
fs.writeFile('File1.txt', 'My Content', function (err) {
  if (err) throw err;

Append “Added Text.” to the end of the file “File1.txt”:

var fs = require('fs');
fs.appendFile('File2.txt', 'Content Added', function (err) {
  if (err) throw err;

Want to improve your abilities and study NodeJS? So what are you still holding out for?

Sign up for a Node.js Certification Course right away!

Use the fs.unlink() method to remove a file with the File System module.

The file given by the fs.unlink() function is removed:

var fs = require('fs');
fs.unlink('File3.txt', function (err) {
  if (err) throw err;
  console.log('File deleted!');

Use the fs.rename() method in the File System module to rename a file.

var fs = require('fs');
fs.rename('File3.txt', 'File4.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed!');

We will be studying NodeJS in more detail in the upcoming chapters, we hope you are finding this tutorial helpful!!

If you face any challenges while reading and learning, do drop out your questions to us on our Community Page!

The post File System Module in Node.js appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples