Skip to main content
read-text-file-using-nodejs

How To Get, Open, Read and Rename File Using Node fs

This Nodejs tutorial help to understand accessing of the file system using nodejs module. The fs() file system module allows you to work with a file system on your system.

As we know, All node.js file system operations have synchronous and asynchronous forms. The fs module enables interacting with the file system in a way modeled on standard POSIX functions.

We’ll cover following opeartion on a file using Node js File System Module:

  • Get a File Stats
  • Open a File
  • Read a File
  • Rename a File

How To Get, Open, Read and Rename File

We’ll learn here to open a file, read a file, read a file stat and rename a file using the Node file system module.

File Modes available

There are following file modes available into fs Nodejs module:

Flag Description
r Open a file for reading. An exception throws if a file does not exist.
r+ Open a file for reading and writing. An exception occurs if the file does not exist.
rs Open a file for reading in synchronous mode.
rs+ Open a file for reading and writing.
w Open a file for writing. The file is created , if it does not exist otherwise truncated.
w+ Open a file for reading and writing. If the file does not exist, it is created or truncated(if it exists).
a Open a file for appending. If the file does not exist, it is created.
a+ Open file for reading and appending. If the file does not exist, it is created.

How To Get File Information Using fs

Following is the syntax of the method to get the information about a particular file.

fs.stat(path, callback)

Take a look at a simple Node file stat example.

const fs = require('fs');

fs.stat('acme.js', function (err, stats) {
  if (err) {
      return console.error(err);
  }
  console.log(stats);
});

The stats variable has all file information.

Open a File using Node fs module

The syntax is following to open a file using open() method, this method is a part of Node js File System Module.

The Syntax:
fs.open(path, flags[, mode], callback)

Let’s use the open() method of the Node fs module to open a file.

// server.js
const fs = require('fs');
fs.open('test.txt', 'r+', function(err, fd) {
  if (err) {
     return console.error(err);
  }
 console.log("File opened successfully!");     
});

If the file exists then, it will open the file in the mode of r+.

Read a File in Nodejs

Reading a file content is a very common operation in the file system, We’ll use readFile() method to read file content. We’ll take an example and understand how we can read the file using the nodejs fs module. We’ll read the file and display the content of the file on the webpage.

const http = require('http');
const fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('test.txt', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/js'});
    res.write(data);
    res.end();
  });
}).listen(3010);

You can access the webpage at this URL: https://localhost:3010

How To Rename a File Using NodeJS Module

We can rename the file using nodejs file system as well. We’ll use rename() method to rename a file. The fs.rename() method renames the specified file.

const fs = require('fs');

fs.rename('test.txt', 'test1.txt', function (err) {
  if (err) throw err;
  console.log('Successfully! File has been renamed!');
});

Leave a Reply

Your email address will not be published. Required fields are marked *