Show List

Node js fs module

fs module is used to work with files. Here are the common usages of fs module:
  • Read File
  • Write File
  • Append File
  • Close File
  • Delete File


Here are the common methods from fs module:
  • fs.open(): opens the file and returns a file descriptor to allow file manipulation
  • fs.readFile(): read the content of a file
  • fs.writeFile(): write data to a file
  • fs.appendFile(): append data to a file. If the file does not exist, it's created
  • fs.close(): close a file descriptor

Reading a File

Here is an example to read a file.
  • File to be read is in the files folder of the same directory where JavaScript file is stored.
readFile.js:
var fs = require("fs");
 
// Asynchronous read
fs.readFile('files/fileA.txt', function (err, data) {
   if (err) {
      return console.error(err);
   }
   console.log("Asynchronous read: " + data.toString());
});

// Synchronous read
var data = fs.readFileSync('files/fileA.txt');
console.log("Synchronous read: " + data.toString());
Running the code:
PS C:\Users\mail2\Downloads\fs-module-examples> node readFile
Synchronous read: Content in fileA
Asynchronous read: Content in fileA

Open a File

Here is an example to open a file. Open method has below signature.

fs.open(path, flags, mode, callback)   

  • path: It is file name including path
  • flags: operation for which the file has to be opened. 
  • r open file for reading. an exception occurs if the file does not exist.
  • r+ open file for reading and writing. an exception occurs if the file does not exist.
  • rs open file for reading in synchronous mode.
  • rs+ open file for reading and writing, telling the os to open it synchronously. see notes for 'rs' about using this with caution.
  • w open file for writing. the file is created (if it does not exist) or truncated (if it exists).
  • wx like 'w' but fails if path exists.
  • w+ open file for reading and writing. the file is created (if it does not exist) or truncated (if it exists).
  • wx+ like 'w+' but fails if path exists.
  • a open file for appending. the file is created if it does not exist.
  • ax like 'a' but fails if path exists.
  • a+ open file for reading and appending. the file is created if it does not exist.
  • ax+ open file for reading and appending. the file is created if it does not exist.
  • Mode: Sets the file mode if the file was created as a result of .open call. Modes are r-read, w-write, r+ -readwrite. Default is readwrite.
  • Callback: It is function with two arguments (err, fileRef). err to handle errors. fileRef is file reference that can be used for subsequent operation.
openFile.js:
var fs = require("fs");

// Asynchronous - Opening File  
console.log("Going to open file!");
fs.open('files/fileA.txt', 'r+', function (err, fileRef) {
    if (err) {
        return console.error(err);
    }
    console.log("File opened successfully!");
    console.log(fileRef.toString());
});
Running the code :
PS C:\Users\mail2\Downloads\fs-module-examples> node openFile
Going to open file!
File opened successfully!
3

Write to a File

Here is an example to write to a file. Write method has below signature:

fs.writeFile( file, data, options, callback )
  • file: path of the file
  • data: data to be written.
  • options: such as encoding, file mode.
  • callback: function to be called when method is executed
writeFile.js :
const fs = require('fs');
 
let data = "This text is going to be written in the file";
 
fs.writeFile("files/fileB.txt", data, (err) => {
  if (err)
    console.log(err);
  else {
    console.log("File written successfully");
  }
});
Running the code:
PS C:\Users\mail2\Downloads\fs-module-examples> node writeFile
File written successfully




Source Code:
https://github.com/it-code-lab/node-js-fs-module-examples

    Leave a Comment


  • captcha text