DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 7 of NodeJS|| File System Module

Hey reader👋Hope you are doing well😊
In the last post we have discussed about REPL in NodeJS. In this post we are going to discuss about File System Module in NodeJS.
So let's get started🔥

Introduction

As we have read in earlier blogs that modules as JavaScript libraries that provides different functionalities for development. Modules can be a single file or a collection of multiple files/folders.
One of the important modules in NodeJS is File System Module (inbuilt) which is used to handle file operations like creating, reading, deleting, etc.

The Node.js file system module allows you to work with the file system on your computer.

When you're building a web app, you might want to upload images, a résumé, videos, or docs to the server. The NodeJS file system has the ability to do all of that and more.
All file system operations can have synchronous and asynchronous forms depending upon user requirements.

How to use File System Module

To use file system module in your project use the following command-:
const fs=require("fs") or import "fs"
The common uses of FileSystem Module are -:

  • Write File

  • Read files

  • Create files

  • Update files

  • Delete files

  • Rename files

  • Append files

  • Close files

As I have mentioned above there are two forms of file system operations-:
Synchronous and Asynchrounous. Let's see about these first.

Synchronous approach

They are called blocking functions as it waits for each operation to complete, only after that, it executes the next operation, hence blocking the next command from execution i.e. a command will not be executed until & unless the query has finished executing to get all the result from previous commands.

Asynchronous approach

They are called non-blocking functions as it never waits for each operation to complete, rather it executes all operations in the first go itself. The result of each operation will be handled once the result is available i.e. each command will be executed soon after the execution of the previous command. While the previous command runs in the background and loads the result once it is finished processing the data.

We will see each of above operation in both synchronous and asynchronous way.

1. Read and Write Files

  • Asynchronous Operation

First of all we will create a file using write operation and will add some text in it.
Image description
So here we are using writeFile() method which accepts the path of file ,data to be added in file and a callback function. It will replace the file if it already exists. Data can be a string, a buffer, an <AsyncIterable>, or an <Iterable> object.
Image description
Our file is created.🙃
This writeFile() method has additional parameter named as options which takes a string, this specifies the encoding of data. By default the encoding is 'utf-8'.

Note-: fs.promises.writeFile() returns a Promise and is part of the modern Node.js API.
Image description
util.promisify(fs.writeFile) converts the callback-based writeFile() to a Promise-based function.
Image description

Now we will try to read the file-:
Image description
Output-:
Image description
We have used readFile() method in which we have passed the path of file, encoding and a callback function. This method will give the content of file.

  • Synchronous Operation

To make the behaviour of above operations Synchronous we have different methods. They are-:
writeFileSync() and readFileSync()
You can try these functions and see the difference. 😊
So this is it for this blog I hope you have understood the two operations very well. In the upcoming blog we are going to read about other operations. Till then stay connected and don't forget to follow me.😊
Thankyou 💜

Top comments (1)

Collapse
 
mrrishimeena profile image
Rishi Kumar

Nice article! From standard practices, in the readFile function, use console.error to handle potential errors.