JavaScript is a popular programming language that lets you handle files in the browser. Let’s learn how!

The environment NodeJS is used for different scripts which include file handling. NodeJS is nothing but an environment to run JavaScript code. I hope you have a basic understanding of NodeJS.

Let’s jump into the tutorial to learn about file handling in JavaScript.

File Handling in JavaScript

Handling files includes different operations like creating, reading, updating, renaming, and deleting. We have to access the files from the system which is not possible for us to write it from scratch. So, NodeJS provides a module called fs (file system) for file handling.

Let’s see different methods from the fs module.

fs.open()

The method fs.open() will take two arguments path and mode.

The path is used to locate the file.

The argument mode is used to open the file in different modes like appending, writing, and reading. 

If you open any file in a specific mode, then you can perform only one type of operation corresponding to the mode you have passed to the method. Let’s see the list of modes and corresponding operations.

Mode Operation
‘r’ Opens a file in reading mode
‘a’ Opens a file in appending mode
‘w’ Opens a file in writing mode
‘a ’ Opens a file in appending and reading mode
‘w ’ Opens a file in writing and reading mode
‘r ’ Opens a file in reading and writing mode

If the file doesn’t exist on the given path, then it will create a new empty file. Let’s see the code for opening a file in different modes.

const fs = require("fs");

fs.open("sample.txt", "w", (err, file) => {
   if (err) throw err;
   console.log(file);
});

The method fs.open() will throw an error if the file doesn’t exist while opening in reading mode. It will create a new empty file in writing and appending modes.

We can perform different operations on the opened file. We will write a complete program at the end of this tutorial after learning some more essential methods from the fs module.

fs.appendFile()

The method fs.appendFile() is used to append the content at the end of the file. If the file doesn’t exist in the given path, then it will create a new one. Append some content to the file using the below code.

const fs = require("fs");

fs.appendFile("sample.txt", "Appending content", (err) => {
   if (err) throw err;
   console.log("Completed!");
});

fs.writeFile()

The method fs.writeFile() is used to write the content to the file. If the file doesn’t exist in the given path, then it will create a new one. Try the below code for writing the content to a file.

const fs = require("fs");

fs.writeFile("sample.txt", "Writing content", (err) => {
   if (err) throw err;
   console.log("Completed!");
});

fs.readFile()

The method fs.readFile() is used to read the content from a file. It will throw an error if the file doesn’t exist in the given path. Examine the following code for the method.

const fs = require("fs");

fs.readFile("sample.txt", (err, data) => {
   if (err) throw err;
   console.log(data.toString());
});

fs.unlink()

The method fs.unlink() is used to delete the file. It will throw an error if the file doesn’t exist in the given path. Have a look at the code.

const fs = require("fs");

fs.unlink("sample.txt", (err) => {
   if (err) throw err;
   console.log("File deleted!");
});

fs.rename()

The method fs.rename() is used to rename the file. It will throw an error if the file doesn’t exist in the given path. Rename the next file with the following code. Be smart!

const fs = require("fs");

fs.rename("sample.txt", "sample_one.txt", (err) => {
   if (err) throw err;
   console.log("File renamed!");
});

Miscellaneous

Now, you are familiar with different file handling methods from the fs (file system) module. You can perform most of the file operations using the methods that you have seen in this tutorial. As we promised, let’s see an example script that opens a file and read content from it using the fs.open() and fs.readFile() methods respectively.

const fs = require("fs");

fs.open("sample.txt", "r", (err, file) => {
   if (err) throw err;
   fs.readFile(file, (err, data) => {
      if (err) throw err;
      console.log(data.toString());
   });
});

Conclusion

That’s it for this tutorial. You can use the file handling methods to automate some of the boring stuff in your day-to-day tasks. I hope you have learned the essential methods for handling files.

Happy Coding 🙂