What you have to learn about Node.js today
One of the good things about being a programmer is that if you are bored with your current technological stack there are always a lot of frameworks and libraries what you can learn. This is what I actually do when I feel bored. During my career I was mostly involved in developing user interface. So I have decided that it is time to learn node js.
Today I want to share my notes on some useful methods in NodeJS FS module. If you worked only with Node frameworks it is a great time learn more about node’s built in modules. Firstly, I’ll give you a bit of theory. Secondly, we will create function which widely uses FS module methods.
I was inspired by course on pluralsight by Samer Buna. I strongly recommend to watch it because this course is about pure NodeJS. Author gives explanations on complex topics in a very simple manner that helps a lot if you are a beginner in node.
FS stands for -”File System”. This module allows a user to perform input and output operations. To start using FS module you just need “require” it as shown by the example.
const fs = require(“fs”);
Every nodejs module has a bunch of useful functions and FS is not an exception. This module contains more than 50 functions. Such wide API allows user to write, delete, update and read information from both a file and directory.
Node js is single threaded and uses concurrence model based on event-loop. That means that each function has synchronous and asynchronous versions.
Let’s walk through some quick examples of async and sync functions in FS module. Feel free to skip this part if you already understand how callbacks work in Node.
- Synchronous function blocks Node JS from executing and returns value to variable “file”. While fs.readFileSync is executing node can’t perform any other operations. Example:
- Asynchronous functions return value as a parameter to a callback that is provided as the second argument to
Asynchronous versions should be used most of the time but for some cases you might find sync version more convenient (read file when starting a server).
Learn programming without practicing is useless. To reinforce the theoretical knowledge I’ve implemented a function which recursively deletes folders structure.
For the sake of simplicity I used only synchronous methods and two build in NodeJS modules —
We have already covered a purpose of FS module. But I haven’t mentioned anything about the path module. This module contains utilities for handling and transforming file paths. Quick example:
First of all let’s implement two helper functions — isDirSync and isFileSync. This functions check the file type.
Inside body of this functions fs.statSync is called with one argument — path to a file. Then it returns an internal Node’s fs.Stat object that contains useful information and methods. In our case this useful methods are isFile() and isDirectory(). Both of them return boolean value.
Now we are ready to implement deleteFolderSync functions. It contains main logic. And it is pretty straight forward.
First step is to check if the file, which function attempts to delete, exists. FS module has a nice method for this — fs.existsSync. It is a synchronous method, but it also has an asynchronous version — fs.exists(path, callback) it is deprecated after version 1.0.0. If you want to check asynchronously that the file exists you better use fs.stat method.
Next step we need to make sure that file is a folder. isDirSync + if statement — easy!
Now main logic can be implemented. This is the place where magic happens and it is the most important part so let’s walk through it step by step.
NodeJS can’t delete folder if it isn’t empty. To check that the folder is empty we can use fs.readditSync. This method does not explicitly checks that folder is empty but returns array of file names. If returned array has zero length that is mean that directory is empty.
Now we need to determine what is a type of each file in folder. If file type is folder then recursively call
deleFolderSync passing correct path to inner folder in other case delete file with
And finally last step is to delete empty folder itself. We can do this by checking how many files left. If zero files left then delete folder with
You can find complete example here.
In this post I’ve covered only a small part of what FS module provides. If you would like to practice in node you can implement this function using only async version of methods.
Thanks for reading. I’d love to hear your thoughts and opinions in the comments section.