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. 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. 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.

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.

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.

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.

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!

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.

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 🙂

Handling files in JavaScript - 56Handling files in JavaScript - 93Handling files in JavaScript - 91Handling files in JavaScript - 72Handling files in JavaScript - 80Handling files in JavaScript - 82Handling files in JavaScript - 40Handling files in JavaScript - 40Handling files in JavaScript - 41Handling files in JavaScript - 68Handling files in JavaScript - 12Handling files in JavaScript - 77Handling files in JavaScript - 17Handling files in JavaScript - 74Handling files in JavaScript - 23Handling files in JavaScript - 9Handling files in JavaScript - 26Handling files in JavaScript - 69Handling files in JavaScript - 80Handling files in JavaScript - 34Handling files in JavaScript - 30Handling files in JavaScript - 63Handling files in JavaScript - 38