Welcome back.
So as I promised in the last video we're now going to see how we use them and we're going to install
a few packages.
Hopefully it's a fun lesson.
The two packages that we are going to install are pretty silly and definitely not the most useful.
But fundamentally we install them the exact same way that will install packages like Express and mongoose
in the next unit.
Before we begin the two objectives that I want to make sure to hit are showing you how to use NPM install
the command and Pimm install to install a package and then also using require to include a package in
a js file.
Let's begin first thing I'm going to do is make a new folder make directory and I'm just going to call
this demo app and going to CD into demo app just like that.
And I'm going to make a new file here.
So touch app.
J.S. and then I'll open that up and I'll just start with a really simple consulate log from J us and
save and run it node.
Yes we're doing this just so that we have a place to install our packages in.
We want to make a directory for them.
We want to have a file that will run them with.
So that's why we have this app dodgiest file inside of the demo app directory.
The first package that we're going to use is something called Catch me.
You can search for it on NPM if you just search for Cat me.
It's the first result and the only result.
Like I said it's a pretty silly package.
What it does is it makes you ASCII art Katz.
So things like this or this here or even this gigantic one here.
But the goal is that it's just one or two javascript methods that you call and they print out a bunch
of text that someone else went through the effort of figuring out how to print these cuts.
The very first thing we need to do is install it.
So it's called Cat dash me.
So then we just need that name and we go back to our terminal here and we run NPM install cat dash me
so that command NPM install is really important.
We'll be doing it constantly every time we started a new application.
Every unit will be installing five or six different packages so you get used to using that pretty quickly
before I hit enter.
I do want to just point out what the demo app currently looks like.
It's empty except for app dot JS.
But as soon as I hit NPM install Cat me it will take a moment.
It's going to NPM servers and finding Cat me and getting the correct files and installing it.
And now if I hit LS here you'll see we actually have a new folder that wasn't there before.
Node modules and I'll open it up here.
There we go node modules and then inside of that we have Cat me and inside of Cat me.
There's a bunch of files and these are the files of that package.
So this is the code that someone else wrote.
You can see these are the different cats that will be printed out.
So node modules is a directory that is automatically created for us whenever we install a package and
all future packages that we install.
So in a moment we'll install a second one.
We'll also live instead of node modules.
So then it will look like node modules has caught me and then our other package which tells knock knock
jokes will be instead of node modules as well.
So that's how we install a package but to use it we need to import it into our application and to do
that we use the require command.
So I'll show you here.
So that looks like this far something I'll just call it something for now.
Equals require cat dash me which is the name of the package when I run this code it will automatically
look for a package called Cat me or a file called Cat me and you will find inside of node modules.
Cat dash me and it knows which code automatically to include.
It has to do with this file called index Dot.
Yes that's a little bit too detailed.
So it automatically includes all of the right code from here.
It grabs everything that's needed and then it sticks it inside a variable called something and that
can be whatever we want it to be called.
Now I'm going to head back to the docs and show you how we can use catenary.
This is the other really important part of package's is using the docs finding out how the packages
work and kept me is very very simple which is why we're using it now.
But some of the things that we use later on we'll have 100 or 200 different methods and they all work
slightly differently and you'll have to be very careful about reading the docs to understand the ins
and outs of some of the functionality but as I said kept me it's very simple.
All we do is require it which we already have done.
And in this case they save it to a variable Cat me and then to get a random cat you just run Cat me
.
So the name of this variable.
Then we add parentheses afterwards.
And that executes a function.
What that tells us is that when we require kept me what we get back is a function that we can execute
just like any other function that we've created.
Just like alert in the browser or consulate log.
When we add parentheses it will execute it.
So let's go ahead and try that.
Now I'll get rid of this.
But log and let's change the name so it's not something let's just call it cat.
I want to show you that the name doesn't have to be kept me.
And then we'll just run cat just like that.
And it did mention that it returns the ASCII art cat.
It doesn't printed out.
So if we just run cat and I run my file now which is node aptest J.S. we don't actually see anything
.
The very fact that we didn't get an error when we ran the cat function tells us that our require worked
.
But of course we want to see the cats.
That's what we're here for.
So I'm just going to cancel that log that so cancel dog cat and save.
And now I'm going to clear and make some space here just so that you can be impressed that clear command
is just a nice way to clear out the terminal.
And now what I want to do is run node apps.
Yes.
And we get a random cat and let's do one more.
There we go.
Fun stuff.
We now have two random cats.
And this is all code that we never wrote.
We're just using someone else's package.
Yes it's simple but it's the exact same concepts to use a more serious package like Express which is
exactly what we have right here.
Var express equals require express or far mongoose equals require mongoose.
So we'll be doing this in just a few videos but it's the exact same syntax.
Var something equals require the name of a package.
So that was kept me.
Now I want to show using this other package called Knock knock jokes and all that it does it's also
very simple.
We install it first and require it and then we execute it.
Knock knock is what they need the variable in this case.
But again it can be called anything.
And when we execute that function it returns knock knock joke.
You want to make sure we're in the same directory as the node modules folder.
We don't want to be outside of this directory like here.
If I install that right here it would make a another node modules directory here so it wouldn't be installed
in the same place as the cat in the package.
So we'll go back into our demo app where we see node modules and we need to run and PM install knock
knock joke.
Let's double check knock knock jokes with the S in the end and hit enter.
It will take a moment.
It goes and finds it and then you can see if I type LS or I do ls node modules we now have two folders
inside if they're cat me and knock knock jokes and I'll open that up.
Let's look at knock knock jokes.
We have this index that genius.
And this is the file where the knock knock jokes are.
It kind of ruins the magic to actually look at it because it will spoil the jokes.
But we can see that there's a joke Saray where each joke is an object and then down here there's this
format joke function that will overturn this.
Knock knock Who's there.
Joked name joked out name who and so on are definitely ruining the magic though.
So let's close out of here and let's import the knock knock joke package into our code here so we'll
just do it up top.
Most of the time you'll see a lot of import statements at the top rather than being spread out across
a file.
They're all at the top.
Like I have here.
These are all of the required statements to import packages.
So we're going to do the same thing.
Var joke equals require.
And then the name of the package which is Naach dash knock dash jokes and save.
And then all we want to do is execute that joke just like that and we'll cancel that log it again because
it just returned to joke.
And if you want to see it we need to print it out.
So now we'll get a cat and a joke if all works well there are two packages have been installed and imported
correctly.
So we save and then we'll expand this and we'll run node apps.
Yes we get our cat and we get this joke.
Knock knock.
Who's there.
ABBE.
ABBE who.
Abby birthday to you.
And one more knock knock who's there.
She is.
She is who.
She's a cute girl.
She's a cute cat too.
All right that's enough of that.
Let's wrap up.
Now let's review some of the basics here.
Those two commands I mentioned in the objectives are NPM install.
So we ran NPM install knock knock jokes and NPM install Cat me whenever we run an install it goes and
finds that code on NPM and once it downloads that code it saves it to a folder directory called node
modules which if it doesn't exist yet it will make node modules the first time.
But then as soon as it does exist it just adds the newer modules into the same node modules directory
.
But just downloading them or installing a package doesn't let us use it.
We still have to include it in our code with the required statement and we require with the name of
the package.
Cat dash me or knock knock jokes.
Save it to a variable and then we can use that variable which is really just storing all of the code
that's coming back from Cat me and knock knock jokes.
We can use that variable throughout the rest of our code.
All right so that's it.
And the next video I'm going to have you do another quick exercise.
This one should be pretty fun because you're going to be using another package one that's actually a
little bit more useful than printing cad or knock knock jokes.
I won't spoil it yet.
I'll see you in the next video.