Welcome back.
Let's get started with the Node.js module system.
And the first module that we are going to talk about is what we call the file system.
So what is the file system?
So we use F s for short, which means file system.
So what is it and what we can do with it?
Well, the first module given to us by the JS provides an API for interacting with the file system.
So it simply means that this module happens to work with files and folders and we can use this module
to create a file.
We can write to a file.
We can read from a file.
We can delete a file, we can rename a file.
We can copy a file.
We can paint a file.
We can truncate a file and we can start a file.
Now you can see how JavaScript is so powerful with JavaScript running on the server.
We can use one language to do all these operations.
So let's get back to Visual City Code and let's get started with how we can create a file using this
file module system.
So as always, we are going to create a folder from scratch and we can blend, use the manual, open
a Visual Studio code and create, or we can use the terminal but advise you to stick to the terminal.
And that way you're going to be familiar with the syntax, with the commands.
But don't worry, I will show you both the manual one or that's using the mouse and then using the terminal.
So as always, you need to find a place.
We are going to keep your files.
So right now I want to navigate on my desktop so I won't know where I am by making use of p.w de.
It means that I'm at the home directory here, so when I search for less meaning I won't know the subfolders
under this home page or my working directory here and I can navigate into desktop.
So here's CD in to desktop, uppercase desktop and now let me click the console and there we go.
So here I am on my desktop so I can create a folder here, right?
Or I can right click here and create a folder says Node Up.
Here on a folder here.
Rename it and say note.
JS.
More juice.
So the next step is you can drag this one and open using your preferred code editor, or I can use the
terminal.
So let me remove this folder and now make use of m k d i r and the name of the folder is going to be
just more juice as I hit enter.
Now my folder has been created and here we go.
So the next step is I need to open this.
Like I said, we can drag and drop inside Visual City code or we can make use of this syntax code and
then dot by make sure that you are inside the folder of Node.js.
So when I make use of s, these are my folders and I want to see the meaning change into that folder.
And here we go, play the console and now make use of code and a period.
And now we see that it has opened for me here as that great.
So because we are going with maybe a couple of modules here, I'm going to create a sub folder which
represents each module.
So again, I can go ahead and do it here.
Right now I am inside the Node.js module.
I can make use of K, D, R, or I will come here and click this folder and says the FS, meaning the
FS module system.
So let me do the same thing using the terminal.
So let me delete the one and now let me get to the terminal.
I'm CD, I R and FS.
Now check it.
Now I have the file here so that is it.
Again, inside this file I can create a file called app or server anything I want so I can do the same
thing on a terminal to make use of touch.
The name can be anything but because we are dealing with f s I make use of fs js now to see that I have
f s been created for me.
Now we are ready to rock, so I think I need to put it inside here.
Sorry I didn't move inside.
Okay, now inside the F.
S.
All right.
So the next step is how can we use the module system?
Let's get rockin.
So if you click on documentation and click on version 18.7 on the left side here, let's search for
file system.
And now this is a file system.
See, scroll down.
And now let's read more by description A by the file system.
These are the methods we can use on that file system.
So let's have a look here.
It says that the FS module enables interacting with the file system in a way module on standard project
functions to use the promise base where I will come back to this very soon.
Well, remember that Node.js is asynchronous by default, so it means that it's heavily relies on callbacks.
With a new update of the s now instead of callbacks, we can use promises also.
So here we have different types of import here.
One is simply means common JS and then ESM meaning the easiest way.
So let's take to the common G syntax.
So click on that.
And now this is how it works.
So the source code simply means that if you want to make use of promise based, then this how we are
going to require the module.
So looking at this syntax here we have a keyword and a variable and then we assigned to required something
here.
So the required here is a function which means that we are making use of this module and that is it.
So let's see how we are going to do it inside Visual Studio code.
So now back to Visual Studio code here.
Now open the FS file and here we go.
So the first step is let's create a file.
So the first uses of F s is what this how I like naming my.
Comment, create file and here we go.
So let's look at the syntax.
So to be able to write a file, we are going to make use of the FS module system.
So let me come up here first and then require the module and cost FS.
It can be anything equal to required meaning.
We want something from the ecosystem of Node.js and here we go code and then F as as that.
So all code modules make use of the absolute input without relative like this one it's going to be f
as I see that.
So here we go.
Now when we require this F is if we try to console.log the F as we see all the methods we have.
So come out to my terminal here.
Now let me see the end to the I mean F.
S module and then run the file node and then F as the G is.
Now if you see all the methods Verrastro, we can run the same command using the terminal here.
So again, let me see the end to the OS is f s for the sorry.
And now let me check the files in it.
I have f s to make use of node and then the f.
F.
If s hit enter.
Now this what we have.
Well, for this one, I don't see it unless I come here.
Expand it.
Nine please see that these are the methods we have on the module we required.
So here we go.
How can we create so make use of file?
And on that we have a method called right file.
We have right file and right file saying and right sync.
Well, some of these mixes of synchronous and asynchronous way.
So let's make use of right file and now we are going to make use of callback here.
So this function takes in three argument.
One is the type of file that we want to create.
So let's say that we want to create, let's say index dot a HTML file.
So our name it as index dot XHTML.
Now the logic here is that we are going to create a file and write some content on it.
So the first argument is the name of the file.
The second argument is the text that we want to write onto this file so we can write simple text cases
that Hello?
Welcome.
So the next agreement is going to be a callback function.
And for this one we can make use of zero function or normal function, but it is anonymous function
here.
So for this one we have access to the error.
So here we are waiting to check if there are some error, then why not?
You go ahead and console.log the error here.
Otherwise, if there is no error, then we can say something to the user that plays the file.
Created.
Perfect.
So now let's run it.
So put your eyes here.
As soon as we run the code, something is going to happen.
So let me clear the console here and now let's make a little bit bigger.
And now let's run the file note.
That is because you want to run this file, make sure you are inside the FS folder.
So hit enter and now something incredible has happened here and it doesn't just create it, but instead
have a look when we open it.
A We have something strange here on the file, meaning that we're able to create a file and write some
content onto the file.
That is awesome.
That is crazy, guys.
So with this one, we can do a lot with it.
Now that we know the syntax of writing or how to create a file, the next video I will show you how
we can read content from a file in the next video.