For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogWeb DevelopmentUnderstanding Node.js File System Module

Understanding Node.js File System Module

Published
05th Sep, 2023
Views
view count loader
Read it in
10 Mins
In this article
    Understanding Node.js File System Module

    About Node.js File system Module

    In any programming language be it java or c #, we need to deal with several file operations, based on business needs. To perform these operations Node.js has provided a specific inbuilt module to help developers which is called File System Module. 

    Node.js File System Module handles file operations like create a file, read a file, delete a file, update a file. All file system operations can have synchronous and asynchronous forms depending upon user requirements 

    To use this File System Module, use the require () method as shown below: 

    We will explore the common use of File System in detail in this article: 

    • Read Files  
    • Write Files 
    • Append Files 
    • Close Files 
    • Delete Files 

    Synchronous and Asynchronous

    In computer science terminology, when we talk about any kind of operation be it file related operations, or operating system related operations, Synchronous and Asynchronous are the terms that would come up. Let’s first understand these two important terms in the context of File System Module.   

    • Synchronous Operation – Synchronous means one operation at a time. Once one action is complete only then, the execution of another action will start. In other terms we can say that the result of one command executed will have to show up for the execution of another command to start. 
    • Sample Code – create a text file called input.txt with the following content  

    Learning Node File System Module  

    Let’s read this file synchronously here file name is main.js 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node main.js 

    Output:  

    • Synchronous read    Learning Node File System Module 
    • Use Case Scenario – When there is no heavy lifting data querying work involved, Synchronous file operation is the best choice. 

    Simple file operation examples involve reading a file and saving it or writing to a file and saving to a system one operation at a time aneeded. 

    • Asynchronous Operation  Asynchronous means multiple operations at a time or in other terms parallel execution of tasks and at the end it calculates of the results of task performed by the userThis operation does not wait for the result of execution of one task instead it performs multiple tasks at a time. 
    • Sample Code  Let’s read input.txt file Asynchronously in this step as follows  

    Output: 

    Asynchronous read: Learning Node File System Module 

    Use Case Scenario – When heavy lifting data querying work is involved Asynchronous file operation is the best choice. 

    Uploading large amount of data to database where this operation takes a lot of time to perform, we can show some kind of loader to the user while work in progress. Any kind of GUI based app is the best use case of Asynchronous operation  

    Real Time Example – Any kind of online booking system is the best example in our daily life where we use this kind of operation.

    Writing Files Synchronously and Asynchronously

    In Above example we can see how we can read files Synchronously and Asynchronously  

    Let’s go through some more sample code where we can write to the file in Asynchronous and Synchronous ways. 

    Sample Code: 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node main.js 

    The simplest way to write a file is to call fs.writeFile() which is an asynchronous operation 

    In the synchronous write operation, we write as follows:  

    Reading Files Through Streams 

    When we talk about files it involves reading a small file or big chunks of files over network which can take lot of time and memory which will be a very expensive operation. To resolve this problem Node.js File system Module has a power concept called Stream. 

    Streams are a way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient way. 

    What makes streams unique, is that instead of a program reading a file into memory all at once like in the traditional way, streams read chunks of data piece by piece, processing its content without keeping it all in memory. 

    The best way to understand steams in our day-to-day life is video streaming platforms like YouTube Videos, Hotstar , Netflix - these services don’t make you download the video and audio feed all at once. Instead, your browser receives the video as a continuous flow of chunks, allowing the recipients to start watching and/or listening almost immediately. 

    Sample Code: 

    Understanding Node.js File System Module

    Above code we start streaming it to the HTTP client as soon as we have a chunk of data ready to be sent. 

    Advantage of using Stream

    1. Memory efficiency: you don’t need to load large amounts of data in memory before you are able to process it 
    2. Time efficiency: it takes significantly less time to start processing data as soon as you have it, rather than having to wait with processing until the entire payload has been transmitted 

    There are 4 types of streams in Node.js:

    1. Writable: streams to which we can write data. For example, fs.createWriteStream()lets us write data to a file using streams. 
    2. Readable: streams from which data can be read. For example: fs.createReadStream() lets us read the contents of a file. 
    3. Duplex: streams that are both Readable and Writable. For example, net.Socket 
    4. Transform: streams that can modify or transform the data as it is written and read. For example, in the instance of file-compression, you can write compressed data and read decompressed data to and from a file. 

    Checking the Status of a File

    Node.js fs.stat() and fs.statSync() method is used to return information about given file or directory  

    • Parameters: This method accepts three parameters as mentioned above and described below: 
      • path: It holds the path of the file or directory that has to be checked. It can be a String, Buffer or URL. 
      • options: It is an object that can be used to specify optional parameters that will affect the output. It has one optional parameter: 
        • bigint: It is a Boolean value which specifies if the numeric values returned in the fs.Stats object are bigint. The default value is false. 
    • callback: It is the function that would be called when the method is executed. 
      • err: It is an error that would be thrown if the method 
      • stats: It is the Stats object that contains the details of the file path. 

    Below examples illustrate the fs.stat() method in Node.js: 

    Sample Code:This example uses fs.stat() method to get the details of the path. 

    Create a sample file called sample.js  

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node sample.js 

    Reference:https://nodejs.org/api/fs.html#fs_fs_stat_path_options_callback 

    Create a sample file called test.js 

    Deleting a File or a Symbolic Link    

    We can remove a file or a symbolic link asynchronously. The method accepts only file path and the callback function. 

    This method does not work on the directory. We can use fs.rmdir() instead. 

    For a synchronous operation, we have fs.unlinkSync()

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node test.js 

    Renaming a File

    To rename a file with File System Module use the fs.rename() method. This method will rename the given file. 

    Sample Code: 

    Create Files 

    The File System module has methods for creating new files: 

    • fs.appendFile() 
    • fs.open() 
    • fs.writeFile() 

    The fs.appendFile() method appends specified content to a file. If the file does not exist, the file will be created: 

    Create a new file name test.js using the appendFile() method: 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node test.js 

    The fs.open() method takes a "flag" as the second argument, if the flag is "w" for "writing", the specified file is opened for writing. If the file does not exist, an empty file is created 

    Create a new, empty file using the open() method: 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location  

    C:\Users\Your Name >node test.js 

    The fs.writeFile() method replaces the specified file and content if it exists. If the file does not exist, a new file, containing the specified content, will be created 

    Create a new file using the writeFile() method: 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node test.js 

    Update Files

    The File System module has methods for updating files: 

    • fs.appendFile() 
    • fs.writeFile() 

    The fs.appendFile() method appends the specified content at the end of the specified file 

    Sample Code: File Name test.js 

    Append "This is my text." to the end of the file "mynewfile1.txt": 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node test.js 

    The fs.writeFile() method replaces the specified file and content: 

    Replace the content of the file "mynewfile3.txt": 

    To Run – node.js should be installed on the system and run the below command on cmd as per your system location 

    C:\Users\Your Name >node test.js 

    Important method of fs module

    MethodDescription
    fs.readFile(fileName [,options], callback)Reads existing file.
    fs.writeFile(filename, data[, options], callback)Writes to the file. If file exists then overwrite the content otherwise creates new file.
    fs.open(path, flags[, mode], callback)Opens file for reading or writing.
    fs.rename(oldPath, newPath, callback)Renames an existing file.
    fs.chown(path, uid, gid, callback)Asynchronous chown.
    fs.stat(path, callback)Returns fs.stat object which includes important file statistics.
    fs.link(srcpath, dstpath, callback)Links file asynchronously.
    fs.symlink(destination, path[, type], callback)Symlink asynchronously.
    fs.rmdir(path, callback)Renames an existing directory.
    fs.mkdir(path[, mode], callback)Creates a new directory.
    fs.readdir(path, callback)Reads the content of the specified directory.
    fs.utimes(path, atime, mtime, callback)Changes the timestamp of the file.
    fs.exists(path, callback)Determines whether the specified file exists or not.
    fs.access(path[, mode], callback)Tests a user's permissions for the specified file.
    fs.appendFile(file, data[, options], callback)Appends new content to the existing file.

    Visit Node documentation for more information on fsmodule. https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback 

    Unlock your coding potential with our unique programming training course. Learn the skills you need to excel in the digital world. Enroll now!

    Using Promise based file system APIs

    Promises API as opposed to the commonly known callback and synchronous based FileSystem methods. 

    In its latest release (Node.js V10), functions of the fs can return promises directly, eliminating the extra step and overhead of the old way. This due to its fs/promises API. 

    The fs/promises API provides the following methods: 

    access, copyFile, open, read, write, rename, truncate, ftruncate, rmdir, fdatasync, fsync, mkdir, readdir, readlink, symlink, fstat, lstat, stat, link, unlink, fchmod, chmod, lchmod, lchown, fchown, chown, utimes, futimes, realpath, mkdtemp, writeFile, appendFile and readFile 

    to read about the promise api’s please go through the official documentation of Node.js latest version.  

    Working with the File System Modules  

    In this article, we looked at some of the most commonly used methods and operations of Node.js File System Module. There are several other rexamples and new additions to the existing list in the newer version of Node.js File system. If you’re interested in learning more about it, you can read the Node.js documentation: https://nodejs.org/api/fs.html 

    Profile

    Jaya Jha

    Author

    I am a full-stack Web Application Developer and passionate about exploring cutting edge technologies. I love to write technical blogs.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon