Getting started with node.js-Day 3

Amitesh Kumar
5 min readJun 20, 2020

--

Hey guys. I hope you all are doing well. This blog is for node.js day 3 where I will try to share some stuff about the ‘File System’ module(fs) in node.js.

Previously we have seen the ‘events’ module in node.js. I am sharing the link for the same if you haven’t learned it.

Today we will talk about the ‘File System(fs)’ module in node.js. This module is inbuilt in node.js. Now when you are working with a project, you need to work with files as well. It can be that you want to write something in a file, delete it, or read it. So how will you do this?.

The answer is that we can do this through the ‘File System’ module. Again I will try to explain this through simple examples that I have implemented.

code for creating a file through fs module

First, we use the require function to include ‘fs’ module which is inbuilt in node.js. Now our task is to make a new file.

before execution of my program

The picture shows how my dashboard looked like before executing this function. Now I will use ‘fs.writeFile()’ function to make a new file automatically. Note that we can make any kind of file through this function it can be .txt file or .js file or anything. Here I took .txt file ‘example.txt’. Within the ‘.writeFile()’ function we have to mention the file we want to make as a parameter, the text we want to put in that file, and then call back function which is fired after making the file and gives an error if it is there or executes our program.

After execution of the program an example.txt file is created.

In the above picture, you can see how an ‘example.txt’ file was created after the execution of my program. So this was a simple way to show to you how files are created by using fs.writeFile() function. This method is an asynchronous method for writing a file.

Note: An asynchronous method first completes the task and then uses the callback function.

Now we will learn about reading a file.

We have created an example.txt file already which has the text “This is an example written in it”. Now we have to read this file.

To read a file we have a function ‘fs.readFile()’ which takes three parameters just as ‘fs.writeFile()’ . First is the file name with the complete path, second is the character encoding which is generally ‘UTF-8’ and third is the callback function which is fired after reading the complete file.

Code for reading a file
example.txt file content
Terminal after reading the file content.

So after executing the ‘fs.readFile()’ we get this output which shows our file has successfully been read.

Now I would like to talk about the renaming of files. We have fs.rename() function in node.js to rename files which again takes three parameters, the original name, the new name (replacing name), and the callback function.

Here is the code for it.

In the above code, as soon as this is executed, the file example.txt is created and is renamed to example2.txt.

Before execution of code
After execution of code

Clearly you can see the file has been renamed.

Now here is a question for you. Suppose, I created a new file called print.js and wrote a content “console.log(“Done”);” in it.

Question
print.js file

What do you think changing the text from the second parameter of fs.writeFile() will do ?

  • The answer is it will overwrite this text which is already there in the file with the new text.
File overwritten

So sometimes there can be a question in your mind like what if I want to append my new text with the already existing text? What if I don’t want to overwrite the file?

The answer to your question is instead of using writeFile() use appendFile(). This file helps you to join your text at the end of the texts which are already written in the previous(print.js) file.

Here is the code showing this:

Code for appending text
This is the output after the execution of the above code.

Lastly, if we want to delete a file that is not in use, we can use unlink() function which takes two parameters, the file to be deleted, and the callback function which is fired after the file is deleted.

code showing how to delete files

So here I end my blog for day 3. I hope it helped you and would love to listen to your appreciation/criticism on this blog. You can connect with me through my email. I am providing my email id for the same.

belikeamitesh@gmail.com

Happy coding!

--

--