Ahmet Meliksah Akdeniz
meliksahakdeniz

meliksahakdeniz

Self-study: Using FS Module

Ahmet Meliksah Akdeniz's photo
Ahmet Meliksah Akdeniz
·Feb 5, 2022·

3 min read

Hello fellow developers! Today I will talk about fs module. Firstly, type require("fs"), this will import the module. You do not need to download this using npm because it is a core module in Node.js. So, if you have downloaded Node.js on your PC, you do not need to download fs module. Assign require("fs") to a variable, so it will be easy to use and read. For instance:

const fs = require("fs")

You do not have to name your variable const fs, however, it is a convention to follow this as it increases code readability.

Write File/ Create File

We can write file using .writeFile() method. This method takes four parameters; first, path (name of the file). Second, what you want to write. Third, encoding ("utf8") is used most of the time. The last one is a callback function. In this case, if writing file fails, log the error to the console.

const fs = require("fs");  // imported fs and assigned it to fs constant

fs.writeFile(
  "albums.json",
  `{"id": 1, "name": "album 1", "artist": "artist 1"}`,
  "utf8",
  (err) => console.log(err)
);

You should see the new file that you wrote like this:

writeFile.PNG

Oh and if you did not initialize package.json just type npm init on your terminal and keep hitting enter. If you want to know more, I talked about it here

Read file

We can read files using the fs.readFile() method, as the name suggests, to read the entire file. The file will display on the terminal. The readFile() method takes three parameters. First, the file that you want to read. Second, encoding, and third a callback function that takes error and data as parameters. If file read fails, throw an error. Otherwise, log the data to the console and also log "File has been read"

fs.readFile("albums.json", "utf-8", (err, data) => {
  if (err) console.log(err);
  console.log(data);
  console.log("File has been read");
});

readFile.PNG

Update File - Append File

We can add code to our files using fs.appendFile() method. It takes three parameters; first, the file that we want to update. Second, the content that we want to add, this literally can be anything. The last one is a callback function.

fs.appendFile(
  "albums.json",
  `\n{ "id": 2, "name": "album 2", "artist": "artist 2" }`,
  (err) => {
    if (err) console.log(err);
    console.log("FILE UPDATED");
  }
);

Here is our album.json file

{ "id": 1, "name": "album 1", "artist": "artist 1" }
{ "id": 2, "name": "album 2", "artist": "artist 2" } // we added this one just now

We can remove a file by using the fs.unlink() method. Compared to the rest of the methods we talked about today, it takes only two parameters. Try to guess... Yep! The first one is the file that you want to unlink, and the second is a callback function. Once this function runs, you will no longer see the file that you just removed.

fs.unlink("albums.json", (err) => {
  if (err) console.log(err);
  console.log("FILE UNLINKED (removed)!");
});

That is all for now. Take care and keep coding

*dev.to/ahmetmeliksah/full-stack-bootcamp-da.. -> article that I talked about npm init

 
Share this