I am a full-stack Web Application Developer and passionate about exploring cutting edge technologies. I love to write technical blogs.
HomeBlogWeb DevelopmentUnderstanding 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:
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.
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:
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 as needed.
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.
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:
When we talk about files it involves reading a small file or big chunks of files over a 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:
Above code we start streaming it to the HTTP client as soon as we have a chunk of data ready to be sent.
Node.js fs.stat() and fs.statSync() method is used to return information about given file or directory
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
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
To rename a file with File System Module use the fs.rename() method. This method will rename the given file.
Sample Code:
The File System module has methods for creating new files:
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
The File System module has methods for updating files:
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
Method | Description |
---|---|
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!
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
Name | Date | Fee | Know more |
---|