Open In App

How to Access the File System in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files.

Prerequisite:

  • Basic knowledge of ES6
  • Basic knowledge of NodeJS

NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses a single-threaded non-blocking I/O model. We can access the file system in NodeJS using some inbuilt modules.

File System: A file is a collection of related information stored in secondary storage Or a file is a collection of similar types of entities that work with the file and manage it also called a File System. 

If you want to know more about the file system refer to this article.

File System Module (fs module): One of the popular inbuilt modules for working with the file system in NodeJS is the file system module which is also called the “fs” module in short. fs module is very powerful for doing any task in NodeJS related to File systems.

Access the File System in Node JS means performing some basic operations on files. This is also known as CRUD operations.

CRUD Operations:

  • C => Create the files
  • R => Read the files
  • U => Update the files
  • D => Delete the file

Basic operations on files using the “fs” module:

Step 1: Create a file with “.js” extension.

Step 2: Add the “fs” module to the codebase.

Syntax:

const fs = require('fs');

After required the fs module, you can perform the following operations on files:

Operation 1: Create a File

Syntax:

const fs = require('fs');
fs.writeFileSync('./{file_name}', 'Content_For_Writing');

The fs.writeFileSync method is used to write something to the file, but if the file does not exist, it creates new files along with writing the contents.

Operation 2: Read the File

Syntax:

const fs = require('fs');
const file_content = fs.readFileSync('./{file_name}', 
    '{content_formate}').toString();

// For show the data on console
console.log(file_content);

The fs.readFileSync method is used to read data from a file, the first argument to readFileSync is the path to the file, and the second argument takes the options {format, flag, etc} and returns the buffer object of the stream. So we can use the buffer String assigned to a variable named file_content using toString() method and after assigning data to file_content the data is shown on the console using console.log() method.

Operation 3: Update the File

Syntax:

const fs = require('fs');
fs.appendFileSync('./{file_name}', " {Updated_Data}");

const file_content = fs.readFileSync(
    './{file_name}', '{file_formate}').toString();

console.log(file_content);

The fs.appendFileSync method is used for updating the data of a file.

Operation 4: Delete a File

const fs = require('fs');
fs.unlinkSync('./{file_name}');

The fs.unlinkSync() method is used to delete the file with passing the file name.

Below is the code implementation for the above operations:

Example:
The filename is index.js

Javascript




const fs = require('fs');
 
/* The fs.writeFileSync method is used
to write something to the file, but if
the file does not exist, it creates new
files along with writing the contents */
fs.writeFileSync('./testfile', 'This is a file');
var file_content = fs.readFileSync(
        './testfile', 'utf8').toString();
 
console.log(file_content);
 
/* The fs.appendFileSync method is used
for updating the data of a file */
fs.appendFileSync('./testfile', " Updated Data");
file_content = fs.readFileSync(
    './testfile', 'utf8').toString();
console.log(file_content);
 
/* The fs.unlinkSync method are used to delete
the file. With passing the file name */
fs.unlinkSync('./testfile');


Try Out the code and run it using node.js by using the following command:

node index.js

Finally, we saw how to access the file system and also tried many operations on it.



Last Updated : 19 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments