Node’s File System module provides functions to work with the local file system of the machine on which node is running.
Node docs state about this module

The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.

All the functions of this module come in asynchronous and synchronous forms. Synchronous functions will have Sync added after their name.
Synchronous functions block execution till the task is complete while with asynchronous functions, further processing continues and the result is returned when the task of this function completes. Result of asynchronous functions is returned by a callback function.
All asynchronous functions take a callback function as their last argument. This callback function is automatically called when the asynchronous function completes.

Using synchronous functions will increase the response time of the application. This is because when there are multiple requests at the same time, node will be able to serve  only one request at a time since synchronous function will block till it completes its task. It is thus advisable to use asynchronous functions.

Importing File System Module
File System module can be imported in another module using require with fs as argument. It returns an object which can then be used to invoke functions of the File System module. Example,

const fs = require(‘fs’);

Creating a directory 
File System module provides a functions mkdir which can be used for creating a folder. It takes a path at which the folder needs to be created, the mode in which it will be created(optional) and a callback function. This callback function accepts an error object which will be populated if there is some error in executing the function. Example,

const fs = require('fs');

// create directory
fs.mkdir('E:/node', function(err) {
   if(err) {
      console.log(err);
   else { 
      console.log('Dir created');
   }
});

As mentioned earlier, there are synchronous and asynchronous versions of every function in this module. Above example was an asynchronous function. Notice the last argument is a callback function which is invoked when the task to be performed by mkdir completes.
Below is an example of synchronous variant of the same function.

// import module
const fs = require('fs');

fs.mkdirSync('E:/node1');

Note that synchronized functions do not take a callback function since they perform their task at the time they are invoked.
Listing files in a directory
File System module provides a readdir function which takes a directory path as argument and returns the list of files in that directory. Example,

// import module
const fs = require('fs');

fs.readdir('./', function(err, files) {
    if(err) {
        console.log('Error in reading directory', err);
    } else {
        console.log('List of files: ', files);
    }
});

Above code takes the current folder path as argument. Since it is an asynchronous function, it also takes a callback function with two arguments.
First argument is the error object which is populated when there is an error in reading the directory. Second object is the list of files in the supplied folder.
Remember that only one of these objects will be populated, other will be null. Output of the above code is

E:\node>node firstprogram.js
List of files: [ ‘firstprogram.js’, ‘usingmodule.js’ ]

Other Functions
File System module provides more utility functions to copy, read, append and delete files; rename files; changing permissions on a file and directory etc.

For a complete list of functions available in File System module head over to node documentation at https://nodejs.org

0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.