Learn nodejs, Directory creation

So far we’ve been using the FS module to work with files, but the FS module also gives us tools for working with directories. Let’s go ahead and navigate to our exercise files. In the start folder, there is an empty JavaScript file named directory.js.

Let’s go ahead and open that up. Now, if we’re going to create a new directory, we want to first use the file system module, so I need to include that. var fs = require (“fs”). The next thing I need to do is invoke the “make directory” function.

Fs.mkdir will make a directory. We could do this synchronously or asynchronously. Right now, I’m going to do this asynchronously. We’re going to create a lib directory, and when that directory is created this function will fire.

If it was not successful and there was an error, an error will be passed to this function. I’m going to go ahead and say, if there is an error, let’s go ahead and log it. Otherwise, let’s go ahead and log “Directory Created.” Great.

We can use the “mkdir function” to create a new directory. Let’s give it a shot. Let’s save this and navigate to our terminal, and let’s run our new file. Node directory. When we do so, we see “Directory Created.” In fact, if I list the file contents of this folder, you will notice that there is now a lib directory in this folder. Pretty cool. Let me try and do this again. Node directory. This time we get an error, and the reason we get an error is because the lib directory already exists.

We see our error is “EExist: file already exists, mkdir.” This means that we cannot create a directory if it is already there. Let’s go ahead and go back to our code, and make our code work a little bit better by checking for the existence of the directory first. The fs has an exist function that we can use to check for the existence of a file or a directory. It’s going to return a true or a false, depending on whether a file or directory actually exists. I’m going to use it directly in an if statement. We’re going to use the synchronous version of this function, and we will check for the library directory.

If our lib directory exists, then we will go ahead and log to the user, “Directory already there.” Otherwise, we will create it. This time we’re going to use the fs.exists to see if the directory exists before we try to create it, because creating an existing directory will throw an error. Let’s go ahead and save this code, and let’s go out into our terminal, and I can clear our terminal contents, and then I’m going to go ahead and run this file: node directory.

When we do so, it says the directory is already there, because the lib directory is already there. We go ahead and remove it. This is how I can remove the directory on my Mac. Rn-rf lib will remove the library directory. Now, when I node directory, we can see that the directory was created, because it’s not already there. If I node directory one more time, we can see that the directory is in fact already there. The file system module also has tools for working with directories.

We can create directories, we can check for the existence of the directories, and we’re going to be able to move and remove directories just like we’ll be able to move and remove files as well.

Leave a Reply

Notify of