Welcome to your very first programming video for the class.
From here on out we're going to be spending our time writing code and building note applications in
Visual Studio code.
Now in this video in particular to kick off the course we're going to talk about one of the most fundamental
features of node.
This is the module system and this is what's going to allow us to take advantage of all of the cool
and interesting things that node provides.
So as an example over here in the docs there are all sorts of modules listed.
Now some as we saw with console are available globally which means we don't have to do anything special
to use them right here as an example.
We just accessed console dot log to use it.
Others require us to actually load them in before they can be used in our scripts.
That's exactly what we're going to explore.
And the module we're going to use to explore this is the file system module listed down below.
This is the module that's going to allow us to access the operating systems file system.
We'll be able to read and write append files and figure out if a given file or directory exists.
All sorts of interesting utilities related to the file system at the table of contents here contains
all of the interesting ways we can use this module inside of our node applications.
And if we start to scroll down we can see there are quite literally hundreds of different ways.
Now at some point we're going to get to a part in the Table of Contents where everything starts with
F. s dot followed by some method name here as an example I have f s dot access all these methods are
listed in alphabetical order and we're going to scroll down to the method we'll be using which is F
S dot right file.
This is the method that's going to allow us to write some data to a file on our file system from the
node j s application.
Now we have two versions of this I have f s dot right file and F S dot right file sync.
The first one is an asynchronous version and the second one is a synchronous version.
And for the moment we're going to stick with the synchronous version.
Don't worry though we will be exploring asynchronous know j s in detail in just a couple of sections.
For now though the goal is to learn about this module system so F Scott right file sync takes into arguments
the name of the file you want to write to and the data you want to write.
So let's go ahead and use it.
I'm gonna head over to the Visual Studio code editor and I will take a moment to delete Hello Dot J
S as this is not gonna make it into any of our applications.
Let's go ahead and trash it and we'll make a brand new folder for our first project which is going to
be a little notes app so I'll call the folder Notes app and in there we're going to make a single j
ust file right here app dot J.
S now down below inside of the terminal.
I am going to switch directories from node course to Notes app by using d space.
Then the folder name notes hyphen app.
Now we're actually running commands from that directory and that's great because we're going to run
node in just a few seconds.
Let's go ahead and actually use F S dot right file sync exactly like we saw in the docs it is F S which
is an object dot the method name is right.
File sync it is indeed a function.
So I am going to call it as such.
Setting up my parentheses and as I mentioned this takes two arguments in this case both are strings.
The first is the name of the file.
The second is the data.
All right let's go ahead and pick a name for our file and I'm going to call this something like notes
Dot T X T.
So for the moment we're going to stick with a simple text file though later on we'll learn how to work
with files of other types.
Now the second argument is going to be the contents to put in that file.
So right here as a string let's go ahead and write out a little message.
This file was created by node j s perfect.
So now we actually have a call to write file sync and we can go ahead and run this script.
The problem is that it's not going to work down below.
Let's prove that I'm going to use node to run a script after a space the script name is app dot J S
which is indeed in the directory we're running commands from the notes app directory.
If I go ahead and hit enter we are not going to see any sort of output that suggests that things went
well.
Right here we have a very long error message.
It contains information about exactly where the error occurred and what exactly is going wrong.
Now later in the course when we explored debugging we'll really break down errors like this for the
moment.
The only important piece is this little message right here F S is not defined.
So the problem is that we're using F S F S though needs to be loaded in and define the file system module.
As I mentioned has to be required by you in the script you're using it in.
So right here we're gonna add a second line before we use the file system module.
We're gonna go ahead and load it in and this is done using the require function that node provides.
And this is at the very core of the module system.
The require function is how we load in other things whether it's a core node module a another file we
created or an NPM module we've chosen to install into use in our projects to load in the file system
module we have to call the require function which is indeed how we load in modules and we pass to it
a single string.
Now when we're looking to load in a core node module we just provide the name of the module and for
the file system it is f s.
Now they require function returns all of the stuff from that module and we have to store that on a variable
right now f s still doesn't exist.
So right here we're going to create a constant called F S and it's value is going to come from the return
value of calling require.
So let's break this down.
We're calling require which is going to load in the F S module the FSA module is built right into node.
So this is going to work for any node script.
What do we do with the return value we store it on the F S variable then down below we call f s dot
right file sync to write some text to the notes Dot T X T file.
Let's go ahead and see if this works down below.
I'm gonna start by clearing the terminal output using the clear command.
If you're on Windows and you're using these standard command prompt you're gonna have to use S.L. s
which is the command for you.
And now we're gonna go ahead and run the app script again.
So node space app that J.
S this time what do we see.
We see nothing it brings us right back to the command prompt asking us to do something else but you'll
notice that over here in the preview for the notes app directory we have a brand new file right alongside
of app dot J S we have notes Dot T X T which got its name from this argument.
And if I crack it open what do we have.
We have our little message in this file was created with node j s which is fantastic.
Now let's go ahead and switch up the text where writing and run the script again.
So over here I'm gonna change the text value for that second argument and go ahead and do the same.
I'll switch it up to something like an introduction.
I'll say my name is Andrew.
Period.
Perfect.
Now we can rerun the script to write some new contents to that file down below.
I'm gonna use the up arrow key to cycle through my previous commands here.
The previous one we ran was node running app at J S then I'll hit enter to run the script again once
again nothing output it down below.
But if I open up notes Dot T X T we have our new text content showing up which is great.
So the right file and the right file sync methods those are responsible for writing data to a file.
If the file doesn't exist as we saw it will be created if the file does exist as we just saw its text
content will be overwritten with the new provided message.
I want to take a quick moment to note that this variable F S it could be called anything we wanted.
The only important thing is that the string we passed to require matches up with what node calls the
module.
Otherwise node is not going to know which module you're trying to load.
So as an example I could call this something like a file system and that would be perfectly valid as
long as I changed my usage down below to file system as well.
Now we have a different name for the variable and the program would work though it's common to stick
with the convention for all node modules we load in there's typically a variable name that everyone
uses and it makes life easier if we all stick with that variable name so we can know what exactly we're
working with across projects.
Now if you're wondering how I knew to load an F S or what the common variable name is or whether the
module even needs to be loaded in at all.
Once again the docs are your best friend.
If we go back over to the documentation and scroll down past the table of contents one of the first
things we're going to see is what we have on line one.
Here we can see exactly what node calls the module based off of the fact that this code is here.
We know it needs to be loaded in unlike console and then they have a common used a variable name which
we can choose to use or choose to ignore.
Though once again it is a best practice to use the name in this case f S..
So at this point we have loaded in our first core node module and we've used it to do something interesting.
Now I want to take a moment to move on to our first challenge for the class.
Whenever I write code in a video you should be following along as well.
I film all of the videos in real time so you can actually write the code I'm writing.
That is a great way to learn something but it's not the best way to make sure you can actually use that
information to solve problems.
So throughout the course in almost every single video I have a challenge where you're going to need
to solve a programming problem based off of what you just learned.
The goal here is to give you as much real world experience as possible actually using node j s on your
own by the end of the course.
You'll actually have a ton of experience writing your own code and building your own apps and you'll
find it much more comfortable to go off after the class and actually use no J.S. whether you're building
your own side project trying to get into freelancing or trying to switch careers and become a node j
ask developer.
So it's important that you follow along with the lessons.
For example you should have this code in your editor.
It's also equally important that you take the challenges seriously.
So here's how they work.
I give you a prompt I give you a second to pause the video.
You go off and solve the problem.
Then you come back and I walk you through the solution.
So let's go ahead and jump into our very first one as always.
I'm going to paste in a little description of what I'd like you to do.
So this is a pretty standard challenge.
Now they're gonna start off easy and they're going to get harder over time as we actually know more
about node and we can do more things right here challenge.
Your goal is to append a message to notes Dot T X T.
So I don't want to override what's here.
I want to add something new on it to the end.
How are you going to do that.
You're going to use a new method from the file system module called append file sync to append to the
file.
Once you've added that in you're going to run the script.
And finally you're going to check your work making sure that the new text you append is showing up on
the end right here.
Now to do that we are going to comment out our existing F S dot in a right file sync call.
We're going to add just that one call in the call to append a file sync and if we head over to the documentation
you can learn more about that.
I'm going to scroll up back to all of those modules and methods we were looking at earlier and if I
go all the way up to F S Dot and I go to the A's I have append file sync right here.
Now append file sync works much the same way as right file sync it needs the name of the file you're
trying to append and it needs the text content you want to append so in the end of the day we want to
append to the file whose name is right here and you can pick whatever message you want as the text to
append.
You could add to the introduction saying something maybe about where you live or how old you are.
If the concept of a challenge it seems a little foreign to you.
Don't worry we're gonna go through this about a hundred different times throughout the course.
So you'll be pretty comfortable with it by the end and as I mentioned things are gonna start off easier
as we know less about node and the challenges will get more complex requiring you to write many lines
of code.
As we progressed through the class for now though it takes some time to knock out all three steps down
below.
You can pause the video when you're done come back and click play
how'd that go.
Very first challenge down.
Hopefully it went well.
If it didn't.
That's OK too.
This is really an introduction to challenges and once again we'll get more comfortable as we do more
of them right here.
The goal was to use f data append file sync.
Now this is already a module we have loaded in so there's no need to load it in again.
Right here we can call f s dot append file and you'll notice that since we've loaded in the FSM module
with visual studio code at least I am getting some helpful suggestions about which methods I have access
to.
Right here I will be using append file sync and calling it as such.
Now what file am I trying to append to.
Well I only have one it is notes dot t t so that will be the value I provide for the first argument
and for the second argument I'm gonna provide the text I want to append I'll say something like I live
in Philadelphia.
Perfect.
Now since we're appending one string right on to the end of the other I'm gonna put a space at the beginning
of the string.
So I have a space between the period up here and the letter I down below.
Now we can go ahead and save app not J S and run our script to test things out.
I'll use the up arrow key and enter to rerun the previous command and if I open up notes dot t t what
do we have.
I have my old message My name is Andrew and I have my new message.
I live in Philadelphia which is excellent.
And there we go.
You've completed your very first challenge.
Once again these are essential to getting real world experience solving problems and working through
your misunderstandings so it's okay if you don't get them all right or it takes you a lot of time to
figure out what's going on.
It's gonna get easier to recall what we covered as we use it more and more now that we're starting to
write code together.
I wanted to remind you that the lecture resources for every lesson of the class contains a zip of the
code up to that point.
So if you're getting stuck and you can't quite figure it out you can always download the zip for that
lesson that contains all of the code changes made and you can compare your code with my code.
You can also head over to links.
Dot need dot I O forward slash node course to view a get Hub Repository that contains every piece of
code created in the class.
That's where we're gonna stop for this one we've now loaded in and used our very first core no J.S.
module in the next video we're gonna continue to talk about the module system and require.
I am excited to get to that.
So let's go ahead and jump right in.