The ‘fs’ Module in Node.js
By: Souvik Paul
Tags: Nodejs, Node Js Tutorial, Programming, Fs, Coding

The ‘fs’ Module in Node.js

Learn Node.js (Part 6)

Photo by Safar Safarov on Unsplash

In this blog we’ll learn about the fs module in node.js. We don’t need to install this module separately, as this is a core module.

To understand the core concepts, we’ll take help of some examples.

Example 1

We’ll create a text file with name myfile.txt with the following data.

Hi there

How are you?

In the same directory, we’ll create another file with name app.js with the following code:

In the above code, we’re reading from and writing to the files synchronously. That means, it will block the execution of other codes while reading and writing.

The above code will give create a file with the name newfile.txt in the same directory and copy all the data from myfile.txt to it. It will also give the following output into the console.

$ node app.js

Hi there

How are you?

Example 2

Now, we’ll create another file app1.js in the same directory with the following code.

In the above code, we’re reading the file myfile.txt asynchronously. For that reason, we have to define a callback function as well.

Since, in asynchronous process, the execution of other codes will not get blocked, that’s why the output will be like this —

$ node app1.js

whoops, I am first

Hi there

How are you?

Example 3

Now, we’ll learn to delete a file synchronously. For this, we’ll create an empty text file with the name yesboss.txt in the current working directory. Also, we’ll create a file app2.js in this directory with the following code.

So, unlinkSync will delete the file synchronously from the current working directory.

Example 4

Now, we’ll learn to delete a file asynchronously. For this, we’ll create an empty text file with the name noboss.txt in the current working directory. Also, we’ll create a file app3.js in this directory with the following code.

Since, unlink is an asynchronous process, we need to define a call back function.

So, we’ll get the following as an output in the console.

$ node app3.js

file deleted asynchronously

Example 5

In this example, we’ll learn to create and delete a directory, synchronously.

For this, we’ll create a file app4.js in the current working directory with the following code.

The above code will synchronously create a new empty directory with the name project in the current working directory.

And then we’ll create another file app5.js with the following code which will synchronously delete this empty directory from the current working directory.

Example 6

In this example, we’ll create a directory new_folder asynchronously and read a file myfile.txt from the current working directory (asynchronously) and write that to another file newfile.txt inside the new directory (asynchronously).

The above program will give the following output.

$ node app6.js

data written

Example 7

Now, we’ll delete the new_folder directory (asynchrnously) which we have created in the previous example. But, the rmdir method will not allow us to delete that directory as it is not empty. For that, we have to empty the directory first.

So, the output of the above code is given below.

$ node app7.js

folder removed

Summary

So, in this article, we have discussed all the basic things that we can do with the help of fs module in Node.js.

References