Node 'fs' File System Module

The fs module in Node.js is essential for working with the file system.

It provides functions for creating, reading, updating, and deleting files and directories.

Introduction to the fs Module

  1. The fs module is part of the Node.js standard library, so there's no need to install it separately.
  2. To use it in your code, include it using the require statement:
const fs = require('fs');

Reading Files

  1. You can read the contents of a file using the fs.readFile() function.
const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
    console.error(err);
    return;
    }
    console.log('File Contents:', data);
});

//Output: File Contents: This is the content of example.txt.

    

Writing to Files

  1. To create or overwrite a file with new content, use the fs.writeFile() function.
const fs = require('fs');

const content = 'This is new content for the file.';
fs.writeFile('newfile.txt', content, (err) => {
    if (err) {
        console.error(err);
        return;
    }
console.log('File written successfully!');
});
//Output: File written successfully!
    

Deleting Files

  1. You can delete files using the fs.unlink() function.
const fs = require('fs');

const filePath = 'fileToDelete.txt';

fs.unlink(filePath, (err) => {
    if (err) {
    console.error(err);
    return;
    }
    console.log('File deleted successfully!');
});
    

Updating the Files

To update a file, you typically follow these steps:

  1. Read the file to get its current content.
  2. Modify the content as needed.
  3. Write the modified content back to the file.
const fs = require('fs');

const filePath = 'updateMe.txt';

// Step 1: Read the file to get its current content
fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
    console.error(err);
    return;
    }

    // Step 2: Modify the content as needed
    const updatedContent = data.replace('old text', 'new text');

    // Step 3: Write the modified content back to the file
    fs.writeFile(filePath, updatedContent, 'utf8', (err) => {
    if (err) {
        console.error(err);
        return;
    }
    console.log('File updated successfully!');
    });
});
    

In this example, we first read the file, then modify the content by replacing "old text" with "new text," and finally write the updated content back to the file.

Working with Directories

  1. You can create, rename, and remove directories using functions like fs.mkdir(), fs.rename(), and fs.rmdir().
const fs = require('fs');

fs.mkdir('myfolder', (err) => {
    if (err) {
    console.error(err);
    return;
    }
    console.log('Directory created successfully!');
});

//Output: Directory created successfully!

    

Reading Directories

  1. To read the contents of a directory, you can use the fs.readdir() function.
  2. It returns an array of filenames.
const fs = require('fs');

const directoryPath = 'myfolder';

fs.readdir(directoryPath, (err, files) => {
    if (err) {
    console.error(err);
    return;
    }
    console.log('Files in the directory:', files);
});