Monday, March 30, 2020

Node.js fs Module

The fs module

We can perform the file operations with Node.js on our computer. The fs module provides all the essential functionalities to access and interact with the file system. This is a built-in module, so it is a component of core Node.js. We do not need to install it separately, just use require() method to include this module into your application.

const fs = require('fs');

All of the general operations to access and manipulate file can be performed with the File System module,

  • Read files
  • Create files
  • Update files
  • Delete files
  • Rename files
There are several methods provided by fs module, Few important methods are listed here,

Method Description
fs.access() test if the file exists and Node.js can access it with its permissions
fs.readFile() read the data in a file
fs.appendFile() append content to a file. If the file does not exist, it will be created
fs.chmod() update the permissions of a file specified by the filename passed.
fs.copyFile() copies content of a file
fs.mkdir() create a new folder set the file mode
fs.close() close a file descriptor
fs.rename() rename a file or folder
fs.writeFile() write content in the file
fs.unlink()  remove a file or a symbolic link
fs.rmdir()  remove a folder

Read Files

To read the content from a file, we can use fs.readFile() function.

Let's create an HTML file and place it in your project folder, Let the name of the file be index.html,

      <title>Test File</title>
       This is an example.
       The server is runing fine!

Now, Create a file as files.js in the same folder to read the HTML(index.html) file, and return the content in this file.

var http = require('http');
      var fs = require('fs');
      http.createServer(function (request, response) {
        fs.readFile('index.html', function(err, data) {
          response.writeHead(200, {'Content-Type': 'text/html'});
      console.log('Serving at http://localhost:3000');

Initiate files.js,

C:\Users\usr\project_name> node files.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:3000

The result will be seen as,

Create Files

We can write content to file using appendFile(), open() or writeFile() method.

In the example below, appendFile() method is used to append the content to the file. A new File is created if it does not exist, 

      var fs = require('fs');
      fs.appendFile('welcome.txt', 'welcome to Node.js', function (err) {
        if (err) throw err;

      console.log('File appended successfully');

The writeFile() method replaces the file and content if it exists. Otherwise, a new file is created if does not exist, and the input content will be placed in this file.

      var fs = require('fs');
      fs.writeFile('welcome.txt', 'welcome to Node.js(content will be overwritten)', function (err) {
        if (err) throw err;
        console.log('Content Saved!');

      console.log('File is overwritten successfully');

Delete Files

We can use unlink() method to remove a file or a symbolic link. To remove a folder rmdir() method can be used.

To delete a file with the File System module, use the fs.unlink() method.

For example to delete welcome.txt

   var fs = require('fs');
      fs.unlink('welcome.txt', function (err) {
        if (err) throw err;
        console.log('file deleted successfully');


Rename A File

A file can be renames with fs module using the rename() method.
For example
To rename "welcome.txt" to “myfile.txt",

   var fs = require('fs');
      fs.rename('welcome.txt', 'myfile.txt', function (err) {
        if (err) throw err;
        console.log('File Renamed successfully');