- [Instructor] We can also create directories
with the fs module.
Let's go into our start files and create a new file
called directory.js.
And again, the first thing we're going to do in this file
is require our fs module.
And I can make a directory by calling fs.mkdir.
The first argument would be the name of the directory
that I would like to create.
We'll call it storage-files.
And then because we're using the asynchronous version
of the make directory function, our callback handler
will only accept an error if it has occurred.
So if there is an error,
I'm going to go ahead and throw the error.
Otherwise I will console.log directory created.
Great, let's go ahead and save this
and come out to the terminal
and we can run our new directory module.
And we can see it says directory created
and notice we have up here storage files.
What happens when I run this module a second time?
Well then we get an error because the storage files
directory already exists.
So the file system also has a function that we can use
to check to see if our directory exists first.
So I will call if fs exists sync and then we will
go ahead and put the name of the directory.
So if this function returns true,
then the directory already exists.
If it's false, the directory does not exist,
so we can go ahead and take our make directory call
and cut that and put it within this else block.
There we go.
Now first we're going to check to see if the storage files
directory exists, if it does we'll just log already there
to the console, otherwise, if it doesn't exist,
we can go ahead and user our makedir call
to create the directory.
Let me go ahead and up the terminal.
And now we can clear the screen
and node directory.
And it says that the directory is already there.
Let's go ahead and remove the directory
and then run our script again.
This time it says directory created.