Understanding Node.js File System basic ops for beginners.

Akhil Goyal
Soopereasy

--

In this article, I’ll be demonstrating you the way, you can use File System to deal with files related operations in Node.js. There are various instances when we need to deal with the files & perform basic operations such as Creating, Reading, Updating, Deleting & Renaming. Well, we’ll be covering all of these basic operations in this article. Let’s first understand the nature of File System module -

File System is a built-in Node.js module & can be imported into the project like this -

var fs = require(‘fs’);

All the methods in this module have synchronous as well as asynchronous forms. Asynchronous methods take first parameter of the callback function as an error and the last parameter as the completion callback. Let’s see through an example -

function fileHandler() {

fs.readFile('testFile.txt', 'utf8', (error, data) => {

if (error) throw error;
console.log(data);

})

}

In the above shown example, we can see that the callback function has two parameters i.e error & data. The first parameter is used to catch any error that occurs & the second parameter returns the response/data after successful execution of the function. Now that we’ve understood the basic usage & nature of…

--

--

Akhil Goyal
Soopereasy

Bubbling thoughts on life, productivity & finances.