In the last lesson we looked at how we can use native Node modules that were bundled with Node.js in 1
order to use libraries of code that we didn't write but somebody else wrote in order to execute commonly 2
used bits of functionality. 3
Now, in this lesson, I want to talk about NPM, which is a package manager for external modules, so modules 4
that anybody could build that you could incorporate into your projects. 5
Now, NPM itself actually stands for Node Package Manager, and it's currently the world's largest collection 6
of these packages of code. 7
So why would you want to use a package manager anyways? 8
Well, let's say that you were building a Lego house, and it's a bit of work putting it all together. 9
Now, let's say that you want to have some trees around your Lego house as well. 10
Now you could spend the time and energy figuring out how to put those trees together, or you could simply 11
go onto NPM and find a tree package, and you can simply incorporate it and add it to your project. And it's 12
exactly the same thing with code. 13
The packages on NPM are bits of reusable code that somebody else wrote, and using NPM you can incorporate 14
those packages into your own projects, 15
saving you the time and effort that's required to write those bits of code yourself. 16
Now NPM actually gets bundled with Node, 17
so when we installed node we already installed NPM. 18
Now, I'm going to delete those two files that we created in the last lesson, 19
and I'm also going to split screen my Hyper.js with Atom, 20
so that means that you guys can see both of them on the same screen, which should make it easier to figure 21
out what's going on. 22
Now I'm going to clear my console so that I start off with a clean slate, and, making sure that you're 23
still inside the intro-to-node directory, 24
we’re going to initialize NPM by saying npm init. 25
And when I hit enter you can see that it's going to run through a utility. It’s kind of like the wizard 26
that you get on Windows when it tries to help you set something up. 27
So the first thing it asked me is what do you want to call your package, and they give you a suggestion inside 28
these parentheses. Are you OK with calling it intro-to-node? 29
Which I'm fine with that, so I'll just hit enter. And then it asks me what's the version of your project, 30
and I'm happy with 1.0.0. 31
Now, the next thing is the description of your project. 32
So let's say, “This is a intro to node project.” And then that becomes the description. The entry point will 33
refer to your main Javascript file, and I've called it index.js. That’s their recommended name as 34
well, so I can just hit enter as well. 35
Test command 36
I'm going to ignore, 37
so I'm just going to hit enter. Git repository we also don't have one, but we will talk about it a lot later 38
on, 39
so we're going to skip this as well, so just hit enter. Keywords, hit enter to ignore. 40
And then author, 41
I'll just put my name, or, 42
in your case, you would put your name. And finally license, 43
ISC, that's fine, hit enter. 44
And now it's compiled all of this data together, and you can see that it's got a name, it’s got a version, 45
it’s got a description, and then it asks you, “Is it OK?” 46
And if you hit enter, then that means it's yes, 47
and that's now done. 48
So that process has created this file called package.json. 49
And this is a description of all of your NPM packages so far. 50
Once we start installing external packages, they will also show up in this file. 51
So enough talking. 52
Let's go ahead and install an NPM external module. 53
So if you head over to npmjs.com, then you've got this big search bar where you can search for 54
all the packages that they contain. 55
And, at the time of making this, there are 650,000 NPM packages, 56
so that's a lot to play with. 57
Now, the one that we're going to check out is we're going to look for something that will generate superhero 58
names. 59
So I'm just going to type in ‘superhero names’, and the first package I get is something called ‘superheroes’. 60
And if you click on it, there's a description that tells you how this package works. 61
So, in order to install it, you write ‘npm install superheroes’. In order to use it, 62
you require the module, and then it has a method called .random that will generate as an output a 63
superhero name. And with all of the NPM modules, you can see that there's extensive documentation on what 64
you can do with it, and how you can install it, related modules, and a whole bunch of things. 65
And I'm going to show you how we can use it. 66
So firstly, as with any NPM module, we first have to install it. 67
So, to install it, we just make sure that we're, again, inside the right directory, 68
so our project folder, 69
and then we write ‘npm install’, and then we write the name of the module, which in this case is called 70
superheroes, plural. 71
So, superheroes. And make sure that you spell it exactly the way that you see it over here, including 72
any capitalizations or dashes that they may have. 73
So now if I hit enter, it will go online and it’ll download that module, and you can see that we now, in our package.json, 74
have a dependency. 75
So our project now depends on a module, and that module is called superheroes 76
version 1.0.0. 77
And you can see over here we get a few warnings and a few notices. 78
Don't worry too much about that. 79
Once we learn about GitHub and Git and version control, then we will get rid of these warnings. 80
At the moment it’s not going to affect our project or our code at all, 81
so don't worry. 82
So, now that we've installed our superheroes package, so if we go into index.js, we're now going 83
to delete this code, which was using an internal Node.js module, 84
and we're going to instead require this superheroes module. 85
And you can see they clearly documented this is how you might use it. 86
So we'll create a var called superheroes, and we're going to set it to equal require the module 87
superheroes. 88
So now that incorporates this module into our current project, and we're able to use it by tapping into 89
this .random function. And all it does is it generates a random superhero name as the output. 90
So let's write ‘superheroes’, which is our superheroes module, . to tap into one of its methods, and the 91
method is called random. 92
Now this as an output generates a random superhero name, 93
so we're going to store that output into a variable, and we're going to call it mySuperheroName. 94
So now we have installed the superhero package, we have included it in our project, and we now are using 95
it to generate a superhero name. 96
Now we should log that superhero name into our command line or our console so that we can see it. 97
So lets simply log mySuperheroName, and lets hit save, and now we can run our code by saying again node 98
index.js. 99
So if I hit enter, you can see we get a name called Free. 100
Its not a very good superhero name. 101
Let's try again. 102
What about the next one? 103
Dragonfly. Oh, I like that. 104
Let's try again. Iron Lad. Hmm, yeah, not feeling it. 105
But in this lesson, you’ve seen how we can go intonations NPM, we can search for packages that might interest us, 106
and very often I will spend a lot of time on here just window shopping, browsing the packages that might 107
inspire my next web project. 108
So the process of incorporating an external package was firstly using npm init in order to initialize 109
a package file, and you can see that this is called package.json. 110
And json is just simply a file format that's used to organize data, and it looks almost a little 111
bit like Javascript objects, where you have a key, and you have a value, and it's just a very simple way 112
of structuring data. 113
And after we initialize this file, we then installed an external package that we wanted, which was called 114
superheroes, and that gets added in as a dependency to our project, 115
so our project now depends on this external package. And it depends on it because we are requiring it 116
inside our project, and then we're using it's random function in order to generate a superhero name. 117
So we literally had to do no work at all in order to have a bit of functionality that will just keep 118
generating new superhero names every single time we run this code. 119
Now as a challenge I want you to do this yourself. 120
So in addition to superheroes. 121
So in addition to superheroes, there's also an NPM package called supervillains, and this allows you 122
to generate supervillain names, which I think is going to be a lot better than the sort of stuff that 123
we're coming up over here. 124
Not looking that great. 125
I'm not very keen on any of these names so far. 126
Maybe we’ll have more luck with the supervillains. 127
So I want you to install this package to our current project, and then I want you to use it to be able 128
to console.log some supervillain names. 129
So pause the video now and complete this challenge. 130
All right. 131
So, after seeing the process of installing an NPM package, I just wanted you to try it out yourself, just 132
so that you can give it a go yourself and see how easy it is. 133
So over here you can see that in order to install, all we had to do is write ‘npm install supervillains’, 134
and we do that inside our command line. 135
So over here, 136
I’ll write ‘npm install supervillains’, making sure that I'm spelling it right. 137
And now it goes onto NPM, fetches the package, and installs it into our project. 138
So now, inside our package.json we now have this dependency listed, and inside our node_modules folder, 139
you can see that there's a whole bunch of things, 140
but we've also got our superhero and supervillain packages. 141
Now the reason why there's so many other packages is because these guys themselves also have dependencies. 142
So our project depends on superheroes. 143
But superheroes will have its own dependencies, and then these packages might have their own dependencies. 144
And it's kind of like Inception. 145
There's just many many layers, and in the end we end up with all of these packages which we have to incorporate 146
in order for all the modules to work. 147
So now, inside our index.js, in order to use this new package, 148
we of course have to require it. 149
So I'm going to do it up here, next to where we've got our superheroes package. 150
So I'm going to say ‘var supervillain = require’, and then the name of the package, which is 151
supervillains. 152
Then I'm going to check their documentation. 153
So, again, it has a method called .random, which generates a random supervillain name. 154
So I can simply write ‘var mySupervillainName = supervillain.random();’. 155
And now, in addition to console logging my superhero name, I'm also going to console.log my supervillain 156
name. 157
So now, if we save this file and we execute it inside our command line, then we can simply say ‘node 158
index.js’. 159
And if I hit enter I should get two names this time. 160
One is my superhero name, which is kind of naff, and the other one is my supervillain name, which sounds 161
a lot better. 162
For some reason I am a lot more drawn to these supervillain names. 163
Captain Boomerang. The Scorpion. Electrocutioner. 164
Yeah I'm really liking this. 165
All right. 166
So, I hope this lesson was interesting, and we've learned a whole bunch of things in this module, including 167
how to get started using Node, using Node’s native packages, as well as NPMs, external packages, and this 168
really is going to be our superpower that we're going to leverage to build really awesome web sites 169
without having to spend a lot of time and energy and effort ourselves. 170
So, in the next module, we’re going to look at a Node framework that's really really popular, and if you're going 171
to be using Node, you're going to be using it. 172
And it's called Express. 173
So for all of that and more, I’ll see you on the next lesson.