1
00:00:00,210 --> 00:00:01,380
Welcome back.

2
00:00:01,380 --> 00:00:06,780
So as I promised in the last video we're now going to see how we use them and we're going to install

3
00:00:06,780 --> 00:00:08,110
a few packages.

4
00:00:08,400 --> 00:00:09,600
Hopefully it's a fun lesson.

5
00:00:09,630 --> 00:00:14,610
The two packages that we are going to install are pretty silly and definitely not the most useful.

6
00:00:14,760 --> 00:00:20,100
But fundamentally we install them the exact same way that will install packages like Express and mongoose

7
00:00:20,340 --> 00:00:21,220
in the next unit.

8
00:00:21,420 --> 00:00:27,090
Before we begin the two objectives that I want to make sure to hit are showing you how to use NPM install

9
00:00:27,450 --> 00:00:34,020
the command and Pimm install to install a package and then also using require to include a package in

10
00:00:34,020 --> 00:00:35,470
a js file.

11
00:00:36,060 --> 00:00:42,600
Let's begin first thing I'm going to do is make a new folder make directory and I'm just going to call

12
00:00:42,600 --> 00:00:48,560
this demo app and going to CD into demo app just like that.

13
00:00:48,780 --> 00:00:50,800
And I'm going to make a new file here.

14
00:00:50,880 --> 00:00:52,850
So touch app.

15
00:00:52,860 --> 00:01:04,740
J.S. and then I'll open that up and I'll just start with a really simple consulate log from J us and

16
00:01:04,740 --> 00:01:09,460
save and run it node.

17
00:01:09,720 --> 00:01:14,820
Yes we're doing this just so that we have a place to install our packages in.

18
00:01:14,940 --> 00:01:16,850
We want to make a directory for them.

19
00:01:16,890 --> 00:01:19,120
We want to have a file that will run them with.

20
00:01:19,140 --> 00:01:23,960
So that's why we have this app dodgiest file inside of the demo app directory.

21
00:01:24,120 --> 00:01:28,290
The first package that we're going to use is something called Catch me.

22
00:01:28,470 --> 00:01:32,010
You can search for it on NPM if you just search for Cat me.

23
00:01:32,070 --> 00:01:34,550
It's the first result and the only result.

24
00:01:35,280 --> 00:01:37,650
Like I said it's a pretty silly package.

25
00:01:37,650 --> 00:01:40,730
What it does is it makes you ASCII art Katz.

26
00:01:41,070 --> 00:01:47,730
So things like this or this here or even this gigantic one here.

27
00:01:48,240 --> 00:01:54,960
But the goal is that it's just one or two javascript methods that you call and they print out a bunch

28
00:01:54,960 --> 00:02:00,730
of text that someone else went through the effort of figuring out how to print these cuts.

29
00:02:00,930 --> 00:02:03,990
The very first thing we need to do is install it.

30
00:02:04,020 --> 00:02:06,850
So it's called Cat dash me.

31
00:02:06,880 --> 00:02:14,490
So then we just need that name and we go back to our terminal here and we run NPM install cat dash me

32
00:02:14,880 --> 00:02:18,000
so that command NPM install is really important.

33
00:02:18,000 --> 00:02:21,000
We'll be doing it constantly every time we started a new application.

34
00:02:21,180 --> 00:02:26,490
Every unit will be installing five or six different packages so you get used to using that pretty quickly

35
00:02:27,210 --> 00:02:28,300
before I hit enter.

36
00:02:28,500 --> 00:02:32,080
I do want to just point out what the demo app currently looks like.

37
00:02:32,370 --> 00:02:34,650
It's empty except for app dot JS.

38
00:02:35,130 --> 00:02:39,940
But as soon as I hit NPM install Cat me it will take a moment.

39
00:02:39,960 --> 00:02:46,410
It's going to NPM servers and finding Cat me and getting the correct files and installing it.

40
00:02:46,650 --> 00:02:52,280
And now if I hit LS here you'll see we actually have a new folder that wasn't there before.

41
00:02:52,410 --> 00:02:55,740
Node modules and I'll open it up here.

42
00:02:55,740 --> 00:03:02,730
There we go node modules and then inside of that we have Cat me and inside of Cat me.

43
00:03:02,890 --> 00:03:07,520
There's a bunch of files and these are the files of that package.

44
00:03:07,770 --> 00:03:09,990
So this is the code that someone else wrote.

45
00:03:09,990 --> 00:03:13,420
You can see these are the different cats that will be printed out.

46
00:03:14,070 --> 00:03:20,760
So node modules is a directory that is automatically created for us whenever we install a package and

47
00:03:20,760 --> 00:03:22,620
all future packages that we install.

48
00:03:22,620 --> 00:03:24,840
So in a moment we'll install a second one.

49
00:03:24,960 --> 00:03:27,660
We'll also live instead of node modules.

50
00:03:27,660 --> 00:03:32,880
So then it will look like node modules has caught me and then our other package which tells knock knock

51
00:03:32,880 --> 00:03:35,850
jokes will be instead of node modules as well.

52
00:03:35,850 --> 00:03:41,490
So that's how we install a package but to use it we need to import it into our application and to do

53
00:03:41,490 --> 00:03:44,000
that we use the require command.

54
00:03:44,340 --> 00:03:45,820
So I'll show you here.

55
00:03:46,290 --> 00:03:51,510
So that looks like this far something I'll just call it something for now.

56
00:03:51,510 --> 00:03:59,970
Equals require cat dash me which is the name of the package when I run this code it will automatically

57
00:03:59,970 --> 00:04:07,000
look for a package called Cat me or a file called Cat me and you will find inside of node modules.

58
00:04:07,000 --> 00:04:11,400
Cat dash me and it knows which code automatically to include.

59
00:04:11,520 --> 00:04:13,610
It has to do with this file called index Dot.

60
00:04:13,640 --> 00:04:15,890
Yes that's a little bit too detailed.

61
00:04:15,990 --> 00:04:18,510
So it automatically includes all of the right code from here.

62
00:04:18,510 --> 00:04:23,280
It grabs everything that's needed and then it sticks it inside a variable called something and that

63
00:04:23,280 --> 00:04:25,560
can be whatever we want it to be called.

64
00:04:25,560 --> 00:04:31,030
Now I'm going to head back to the docs and show you how we can use catenary.

65
00:04:31,140 --> 00:04:36,300
This is the other really important part of package's is using the docs finding out how the packages

66
00:04:36,300 --> 00:04:40,590
work and kept me is very very simple which is why we're using it now.

67
00:04:40,770 --> 00:04:45,690
But some of the things that we use later on we'll have 100 or 200 different methods and they all work

68
00:04:45,690 --> 00:04:50,400
slightly differently and you'll have to be very careful about reading the docs to understand the ins

69
00:04:50,400 --> 00:04:56,450
and outs of some of the functionality but as I said kept me it's very simple.

70
00:04:56,500 --> 00:04:59,520
All we do is require it which we already have done.

71
00:04:59,770 --> 00:05:06,090
And in this case they save it to a variable Cat me and then to get a random cat you just run Cat me

72
00:05:06,080 --> 00:05:06,280
.

73
00:05:06,380 --> 00:05:08,180
So the name of this variable.

74
00:05:08,540 --> 00:05:10,370
Then we add parentheses afterwards.

75
00:05:10,490 --> 00:05:12,210
And that executes a function.

76
00:05:12,220 --> 00:05:18,170
What that tells us is that when we require kept me what we get back is a function that we can execute

77
00:05:18,410 --> 00:05:20,450
just like any other function that we've created.

78
00:05:20,440 --> 00:05:23,600
Just like alert in the browser or consulate log.

79
00:05:23,870 --> 00:05:26,210
When we add parentheses it will execute it.

80
00:05:26,600 --> 00:05:27,660
So let's go ahead and try that.

81
00:05:27,670 --> 00:05:29,910
Now I'll get rid of this.

82
00:05:29,920 --> 00:05:35,960
But log and let's change the name so it's not something let's just call it cat.

83
00:05:36,110 --> 00:05:38,790
I want to show you that the name doesn't have to be kept me.

84
00:05:39,250 --> 00:05:42,020
And then we'll just run cat just like that.

85
00:05:42,290 --> 00:05:45,930
And it did mention that it returns the ASCII art cat.

86
00:05:45,980 --> 00:05:47,040
It doesn't printed out.

87
00:05:47,180 --> 00:05:55,240
So if we just run cat and I run my file now which is node aptest J.S. we don't actually see anything

88
00:05:55,250 --> 00:05:56,050
.

89
00:05:56,060 --> 00:06:01,780
The very fact that we didn't get an error when we ran the cat function tells us that our require worked

90
00:06:01,800 --> 00:06:01,990
.

91
00:06:02,200 --> 00:06:04,120
But of course we want to see the cats.

92
00:06:04,190 --> 00:06:05,390
That's what we're here for.

93
00:06:05,540 --> 00:06:11,990
So I'm just going to cancel that log that so cancel dog cat and save.

94
00:06:12,470 --> 00:06:19,880
And now I'm going to clear and make some space here just so that you can be impressed that clear command

95
00:06:19,880 --> 00:06:22,530
is just a nice way to clear out the terminal.

96
00:06:22,630 --> 00:06:25,280
And now what I want to do is run node apps.

97
00:06:25,340 --> 00:06:27,010
Yes.

98
00:06:27,010 --> 00:06:31,980
And we get a random cat and let's do one more.

99
00:06:32,000 --> 00:06:33,490
There we go.

100
00:06:33,500 --> 00:06:34,190
Fun stuff.

101
00:06:34,220 --> 00:06:35,620
We now have two random cats.

102
00:06:35,840 --> 00:06:38,160
And this is all code that we never wrote.

103
00:06:38,320 --> 00:06:40,090
We're just using someone else's package.

104
00:06:40,220 --> 00:06:47,260
Yes it's simple but it's the exact same concepts to use a more serious package like Express which is

105
00:06:47,260 --> 00:06:48,850
exactly what we have right here.

106
00:06:48,880 --> 00:06:55,790
Var express equals require express or far mongoose equals require mongoose.

107
00:06:55,850 --> 00:06:59,700
So we'll be doing this in just a few videos but it's the exact same syntax.

108
00:06:59,720 --> 00:07:04,790
Var something equals require the name of a package.

109
00:07:04,880 --> 00:07:06,470
So that was kept me.

110
00:07:06,470 --> 00:07:12,110
Now I want to show using this other package called Knock knock jokes and all that it does it's also

111
00:07:12,110 --> 00:07:13,150
very simple.

112
00:07:13,190 --> 00:07:17,070
We install it first and require it and then we execute it.

113
00:07:17,090 --> 00:07:19,840
Knock knock is what they need the variable in this case.

114
00:07:19,970 --> 00:07:21,460
But again it can be called anything.

115
00:07:21,560 --> 00:07:25,370
And when we execute that function it returns knock knock joke.

116
00:07:25,500 --> 00:07:29,020
You want to make sure we're in the same directory as the node modules folder.

117
00:07:29,020 --> 00:07:33,040
We don't want to be outside of this directory like here.

118
00:07:33,310 --> 00:07:38,030
If I install that right here it would make a another node modules directory here so it wouldn't be installed

119
00:07:38,020 --> 00:07:40,230
in the same place as the cat in the package.

120
00:07:40,370 --> 00:07:48,140
So we'll go back into our demo app where we see node modules and we need to run and PM install knock

121
00:07:49,460 --> 00:07:51,350
knock joke.

122
00:07:51,400 --> 00:07:56,470
Let's double check knock knock jokes with the S in the end and hit enter.

123
00:07:56,480 --> 00:07:57,700
It will take a moment.

124
00:07:57,860 --> 00:08:05,570
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

125
00:08:05,560 --> 00:08:10,020
inside if they're cat me and knock knock jokes and I'll open that up.

126
00:08:10,060 --> 00:08:11,990
Let's look at knock knock jokes.

127
00:08:12,280 --> 00:08:14,090
We have this index that genius.

128
00:08:14,210 --> 00:08:17,070
And this is the file where the knock knock jokes are.

129
00:08:17,140 --> 00:08:20,810
It kind of ruins the magic to actually look at it because it will spoil the jokes.

130
00:08:21,130 --> 00:08:27,380
But we can see that there's a joke Saray where each joke is an object and then down here there's this

131
00:08:27,380 --> 00:08:29,890
format joke function that will overturn this.

132
00:08:29,890 --> 00:08:31,420
Knock knock Who's there.

133
00:08:31,490 --> 00:08:36,280
Joked name joked out name who and so on are definitely ruining the magic though.

134
00:08:36,430 --> 00:08:42,220
So let's close out of here and let's import the knock knock joke package into our code here so we'll

135
00:08:42,230 --> 00:08:43,370
just do it up top.

136
00:08:43,460 --> 00:08:47,940
Most of the time you'll see a lot of import statements at the top rather than being spread out across

137
00:08:47,940 --> 00:08:48,770
a file.

138
00:08:48,820 --> 00:08:49,790
They're all at the top.

139
00:08:49,880 --> 00:08:50,760
Like I have here.

140
00:08:50,840 --> 00:08:55,040
These are all of the required statements to import packages.

141
00:08:55,030 --> 00:08:56,430
So we're going to do the same thing.

142
00:08:56,530 --> 00:09:00,260
Var joke equals require.

143
00:09:00,400 --> 00:09:07,310
And then the name of the package which is Naach dash knock dash jokes and save.

144
00:09:07,310 --> 00:09:14,210
And then all we want to do is execute that joke just like that and we'll cancel that log it again because

145
00:09:14,240 --> 00:09:15,940
it just returned to joke.

146
00:09:16,120 --> 00:09:18,340
And if you want to see it we need to print it out.

147
00:09:18,620 --> 00:09:25,150
So now we'll get a cat and a joke if all works well there are two packages have been installed and imported

148
00:09:25,150 --> 00:09:26,010
correctly.

149
00:09:26,330 --> 00:09:34,010
So we save and then we'll expand this and we'll run node apps.

150
00:09:34,060 --> 00:09:39,350
Yes we get our cat and we get this joke.

151
00:09:39,500 --> 00:09:40,130
Knock knock.

152
00:09:40,120 --> 00:09:41,290
Who's there.

153
00:09:41,300 --> 00:09:42,170
ABBE.

154
00:09:42,160 --> 00:09:43,330
ABBE who.

155
00:09:43,510 --> 00:09:45,420
Abby birthday to you.

156
00:09:46,310 --> 00:09:48,650
And one more knock knock who's there.

157
00:09:48,670 --> 00:09:49,520
She is.

158
00:09:49,690 --> 00:09:50,610
She is who.

159
00:09:50,750 --> 00:09:52,880
She's a cute girl.

160
00:09:52,880 --> 00:09:55,030
She's a cute cat too.

161
00:09:55,040 --> 00:09:56,300
All right that's enough of that.

162
00:09:56,600 --> 00:09:57,270
Let's wrap up.

163
00:09:57,290 --> 00:10:00,310
Now let's review some of the basics here.

164
00:10:00,620 --> 00:10:04,630
Those two commands I mentioned in the objectives are NPM install.

165
00:10:04,660 --> 00:10:11,990
So we ran NPM install knock knock jokes and NPM install Cat me whenever we run an install it goes and

166
00:10:11,990 --> 00:10:18,430
finds that code on NPM and once it downloads that code it saves it to a folder directory called node

167
00:10:18,430 --> 00:10:23,160
modules which if it doesn't exist yet it will make node modules the first time.

168
00:10:23,240 --> 00:10:28,490
But then as soon as it does exist it just adds the newer modules into the same node modules directory

169
00:10:28,500 --> 00:10:28,780
.

170
00:10:28,970 --> 00:10:32,680
But just downloading them or installing a package doesn't let us use it.

171
00:10:32,680 --> 00:10:38,500
We still have to include it in our code with the required statement and we require with the name of

172
00:10:38,500 --> 00:10:39,260
the package.

173
00:10:39,470 --> 00:10:41,350
Cat dash me or knock knock jokes.

174
00:10:41,440 --> 00:10:46,940
Save it to a variable and then we can use that variable which is really just storing all of the code

175
00:10:47,140 --> 00:10:50,090
that's coming back from Cat me and knock knock jokes.

176
00:10:50,170 --> 00:10:53,200
We can use that variable throughout the rest of our code.

177
00:10:53,200 --> 00:10:54,480
All right so that's it.

178
00:10:54,540 --> 00:10:57,490
And the next video I'm going to have you do another quick exercise.

179
00:10:57,560 --> 00:11:01,510
This one should be pretty fun because you're going to be using another package one that's actually a

180
00:11:01,510 --> 00:11:04,840
little bit more useful than printing cad or knock knock jokes.

181
00:11:04,850 --> 00:11:06,020
I won't spoil it yet.

182
00:11:06,050 --> 00:11:06,980
I'll see you in the next video.
