DEV Community

Cover image for Getting started with NodeJS file system
Mahmoud Mansour
Mahmoud Mansour

Posted on

Getting started with NodeJS file system

Hi Everyone, in this article I will talk about NodeJS File system and how it works

First what is the file system or 'fs' in NodeJS: it's a module comes with NodeJS and it allows us to work with file system on our computer.

and the use cases for the file system module are read, write, update, delete, rename files.

So let's get started with file system in NodeJS.

There are two types of file system Synchronous and Asynchronous, and we will talk about the asynchronous type because that is what we will use most of the time and later I will talk about. async and await cause they are better at handling it and I will tell you why.

To use file system first we need to import it
const fs = require('fs')
after importing the module we have many methods and I will talk about the ones you use for reading and writing asynchronously.
to read file we use fs.readfile method and this method takes the path of the file, encoding and the callback function that will be called with the file data

const fs = require('fs')

fs.readFile("/path-of-file", "utf-8", (err,result) => {

if(err){
console.log(err);
return ;
}
console.log(result);

} )
Enter fullscreen mode Exit fullscreen mode

Let's break this code:

  1. First we call the 'fs' or Node file system module
  2. and then we use the readFile API to read a file from the computer file system. 3.The readFile method takes 3 parameters the first one is the path of the file, second one is the encoding type, and the third one is the callback function with two parameters err, and result

Now let's talk about writing files, we have another method for writing and we can use it to create or update exciting file

const fs = require('fs')

fs.writeFile("/path-of-file", "Content to be written" (err) => {

if(err){
console.log(err);
return ;
}
// successfully written our file
} )
Enter fullscreen mode Exit fullscreen mode

Let's break this code down.

  1. First we call the 'fs' or Node file system module

  2. we use the writeFile method to write a fill or update an exciting one.

  3. We give it a path for the file to be written, the second parameter is the content that we want to write in the file and the third parameter is the callback function where we give it only the error parameter as it doesn't return anything.

after that we can check if the file has been written on the path we set for it. and congrats now you can read and write using Node file system.

Note: the default behavior of writeFile API is to replace the file with new one if you want to update the file you need to set a flag
and the flag can be set as a parameter for the writeFile API

const fs = require('fs')

fs.writeFile("/path-of-file", "Hello this is me writing to the file", {flag:'a'} (err) => {

if(err){
console.log(err);
return ;
}
// successfully written our file
} )
Enter fullscreen mode Exit fullscreen mode

Notice that we have added a flag as third parameter and we have few type of flags you can use


r+ - This flag opens the file for reading and writing

w+ - This flag opens the file for reading and writing and it also positions the stream at the beginning of the file

a - This flag opens the file for writing and it also positions the stream at the end of the file

a+ - This flag opens the file for reading and writing and it also positions the stream at the end of the file


And that's it with that we reach the end of our tutorial, I hope you enjoyed and learned something useful

Top comments (0)