How to use the fs module in Node.js?

File System Module in Node.js

The File System (FS) module in Node.js is an effective tool for handling files and offers a variety of features. You may use it to create, read, update, and remove files and directories in your computer’s file system.

Introduction

The widely used V8 JavaScript engine from Chrome serves as the foundation for Node.js, an open-source runtime environment. One of the numerous modules offered by Node.js that enables working with your computer’s file system is the File System module, which enables developers to run JavaScript code on the server side.

Installing the File System Module in Node.js

Installing the fs module is a prerequisite for using it in your Node.js application. You may accomplish this by adding it to your package.json file or by using the Node Package Manager (NPM).

npm install fs

Creating and Writing to a File

The fs.writeFile() function in Node.js may be used to create a new file. This function requires two arguments: the path to the file you want to create and the content you want to write to the file.

Here’s an illustration:

const fs = require('fs');

fs.writeFile('example.txt', 'Hello World!', function (err) {
  if (err) throw err;
  console.log('File created!');
});

In this illustration, we make a new file called example.txt and enter the text “Hello World!” into it.
The fs.writeFile() method may activate the optional callback function when it finishes the process.

Reading from a File

Developers can use the fs.readFile() function in Node.js to read the contents of a file. This function requires two parameters: the location of the file you want to read and an optional encoding that defines the character encoding to use. Here’s an illustration:

const fs = require('fs');

fs.readFile('example.txt', 'utf8', function (err, data) {
  if (err) throw err;
  console.log(data);
});

In this example, we use the fs.readFile() function to read the contents of the example.txt file and log the information to the console.

Updating a File

The fs.appendFile() function in Node.js can be used to modify a file’s content. The path of the file you want to update is the first parameter in this function. The second parameter is the content you want to add to the file. Here’s an illustration:

const fs = require('fs');

fs.appendFile('example.txt', ' Node.js is awesome!', function (err) {
  if (err) throw err;
  console.log('File updated!');
});

Deleting a File

The fs.unlink() function in Node.js may be used to remove a file. You only need to input the path to the file you want to remove for this function. Here’s an illustration:

const fs = require('fs');

fs.unlink('example.txt', function (err) {
  if (err) throw err;
  console.log('File deleted!');
});

In this example, we use the fs.unlink() function to remove the example.txt file.

Creating a Directory

The fs.mkdir() function in Node.js may be used to create a new directory. This function only accepts the path to the directory you wish to create as its input. Here’s an illustration:

const fs = require('fs');

fs.mkdir('example', function (err) {
  if (err) throw err;
  console.log('Directory created!');
});

In this example, we use the fs.mkdir() function to create a new directory called an example.

Reading from a Directory

You can use the fs.readdir() function in Node.js to read the contents of a directory. This function only requires the path to the directory that you wish to read as its parameter.

Here’s an illustration:

const fs = require('fs');

fs.readdir('example', function (err, files) {
  if (err) throw err;
  console.log(files);
});

In this illustration, we use the fs.readdir() function to read the contents of the sample directory and log the information to the console.

Deleting a Directory

You may use the fs.rmdir() function in the Node.js fs module to remove a directory. You only need to input the path to the directory you wish to remove for this function. Here’s an illustration:

const fs = require('fs');

fs.rmdir('example', function (err) {
  if (err) throw err;
  console.log('Directory deleted!');
});

In this example, we use the fs.rmdir() function to remove the sample directory.

Renaming and Moving Files

The fs.rename() function in Node.js may be used to rename or transfer a file. The path of the file you wish to rename or transfer is the first parameter in this function. The second parameter is the new path for the file. Here’s an illustration:

const fs = require('fs');

fs.rename('example.txt', 'newExample.txt', function (err) {
  if (err) throw err;
  console.log('File renamed/moved!');
});

Conclusion


The Node.js File System module is a powerful tool that enables working with the file system on your computer. This article examined the most popular features of the File System module, including creating and writing to files, reading from files, updating files, deleting files, creating directories, reading from directories, deleting directories, and renaming/moving files.

Follow Us on

https://www.linkedin.com/company/scribblers-den/
https://www.facebook.com/scribblersden.blogs

Read More
https://scribblersden.com/benefits-of-business-process-automation/

Thank You

Related Post

Leave a Reply

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