Now when we added third-party libraries
to our browser-side code,
no matter if it was CSS or JavaScript code,
we worked with link or script HTML elements
where we simply used an external URL resource path,
so where we simply included an external package.
Now, that won't work here
because here we don't have a HTML document
into which we import anything.
Instead here, we have code
that will never reach the browser
and that will instead be executed here
on the remote machine if we deploy this code.
At the moment, on our local machine.
And therefore, we need a different mechanism
for including external packages.
Now, we are including a built-in package
with the require method
and therefore, you could think
that you maybe can also require some external package.
For example, something like https
and then we wanna include the Express package,
so maybe they have a URL like expressjs.com
from which we can load that package.
But that's not how it works.
Instead, including third-party packages
is such a common task in NodeJS development,
in advanced web development in general to be precise,
NodeJS has a built-in mechanism
for installing third-party packages into this project
and for then using these packages in the code.
And for that, we'll have to open the terminal again,
this integrated terminal,
which is navigated into this project folder here,
and in this terminal,
we will now run the npm command.
Before we executed the node command
to run our app.js file with NodeJS.
Now we'll use a different command,
which was also made available
by us installing NodeJS on our system,
and that's the npm command.
Npm stands for Node Package Manager,
and it is an extra tool,
an extra command that was installed together
with NodeJS and it is a command
that helps us install third-party packages
into this project.
For that, we'll have an npm install command
but before we can execute this sub-command here,
we first of all have to run npm init
to turn this standard folder on our system
into a NodeJS-managed project
into which we then will be able
to install third-party packages.
And therefore, you now need to run npm init
in this project folder,
and this will then ask you a couple of questions.
It will, for example, prompt you
to choose a package name.
Now, the package name is up to you,
and it's suggesting a default to us here in parentheses.
That's the default value it will use.
And that's simply my folder name.
And I'm fine with that,
so I will just hit Enter without entering anything
to confirm that default.
It then wants us to assign a version to our project,
and that's not too important here
since we won't publish this
as a standalone third-party package
to other developers,
hence versioning doesn't matter to us here.
Hence, we can also confirm the default 1.0.0
by again hitting Enter.
Now we can enter a human-readable description
but you can also omit this by hitting Enter again.
Alternatively, you could enter something
like getting started
with Node and Express but that's up to you.
Now I'll confirm this with Enter
and now we have to define an entry point.
Basically, the main file, the main code file
that should be executed once this application runs,
and here we can confirm that default app.js,
though this is also not too important right now.
Now, you can omit this test command
and just not enter anything and hit Enter,
and then here I'm also asked for a Git repository
where I wanna store my code.
You will probably not be asked to provide that
if you're not using Git,
which you probably aren't.
You probably don't even know what that is, and that's fine.
We'll dive into that later in the course.
And hence, you can ignore that setting.
I will just use my default here.
Now, you can also add some extra keywords here
if you wanna tag your project
but again, that's not too important here.
And hence, I'll hit Enter again.
Then you can enter your name as an author
of this project
to make it clear who's working on that project.
And you can also choose a license,
which only really matters if you plan
on distributing that code,
and if you wanna make that code usable by others as well.
I'll go for MIT here but it really doesn't matter.
Last but not least, you see a summary
and by hitting Enter one last time,
you confirm all of that.
Now, this did now create such a package.json file,
and this is an important file.
In there, I'll remove this repository setting,
which you might not even have.
And then I have a pretty lean file here.
Also get rid of bugs and homepage though
so that my file looks something like that.
Now, that's basically a configuration file
for this project, for this NodeJS project
and we need such a configuration file
to then successfully install third-party packages.
You can always tweak the information
which you entered before in the command line directly here
in the package.json file
if you, for example, wanna change your description here.
Now, as a little side note, JSON is a file format.
It's basically a standard text file
but following certain formatting rules
and it looks a bit like a JavaScript object
if you take a closer look.
The main difference is that all your keys
are wrapped by double quotes,
and then you have various values stored for those keys.
And that is a format which we'll also see again later
in the course since it's also a standard format
for submitting data between a browser and a server,
for example, or vice versa.
For the moment, we'll not use it for that though
but we need this config file for this project
to now also use the npm install command
to install third-party packages.
And that was the main point of this lecture.
We wanted to install Express.js.
And for that, I'll reopen that terminal,
this built-in command line,
and now with that package.json file that was created,
we can now run npm install,
and then install express.
Now, you can't enter any arbitrary name here.
Instead, you have to install one of the third-party packages
that is listed on the official npm repository,
the npm library.
For that, you can simply google
or search for npm,
and you should find npmjs.com.
On this page, you can find all the packages
that you can install through npm.
And I will warn you, there are hundreds
of thousands of packages.
So you typically don't go there and look
for packages that sound interesting.
Instead, you typically encounter packages
that you want to install in tutorials, courses,
blog articles or something like that.
Here if you search for Express,
you will find that Express package
that we did just install.
Here it is.
And here you can learn a bit more about it.
You see how often it's being used.
This is a super popular package,
which is being used a lot.
And if you wonder how you would know
that this is a good package to install,
well, you'll learn about it in this course.
And over time, as you gather more and more experience
as a web developer,
you will know more and more common packages
that solve certain problems for you.
And here Express.js will make building NodeJS backends
as a whole easier for us.
That's why we installed it or why we will install it
by running npm install and then this package name.
Here I can now hit Enter
and this will now download this package
from the npm repository
and install it into this local project.
And you can see it there.
This node_modules folder was added here
and that is a folder
that includes all the installed packages.
Now, if you open it,
you will see quite a bit of packages here,
quite a lot of packages,
and you will also see your express package here.
But also a couple of other packages.
The reason for that is that Express itself
also has dependencies.
So express is a package that depends on other packages.
And those other packages also might depend
on yet other packages.
And because of that,
when you run npm install,
you actually install not just the package itself,
express, but also all the dependencies this package needs
to work correctly.
And all those packages are stored in node_modules.
And we will then be able to include the installed packages
from there and use them in our own code.
Now, in addition to this node_modules folder,
in package.json, you'll find this new dependencies entry,
which was not there before.
And that's simply a list
of all the third-party packages you installed
into this NodeJS project.
And whenever you run the npm install command
and you install a new package,
this entry will change and list this new package
with the version that was installed.
This version can matter if you work on a bigger project
and some packages change over time,
and you maybe wanna lock in a specific version
to ensure that your project doesn't crash
because some package changed
and maybe doesn't work as before anymore.
That's why you can lock in a version here.
But here you can simply leave the version
that was installed for you,
even if it's not the same version as you see
in this video.
This won't be a problem.
We also got the package-lock.json file
that's basically a more detailed registry
of all the packages that were installed
and their versions, and you can ignore that.
You should not delete node_modules or package-lock
or package.json though
because if you would do that,
this project wouldn't work correctly anymore,
and you would not be able
to use the third-party package anymore.
You can theoretically delete node_modules
but then you will always have to rerun npm install
without express, just like that,
to reinstall all the dependencies that are listed
in package.json, and then if you refresh here,
you'll see node_modules again.
That can be useful
because the node_modules folder can get pretty big
since it contains a lot of packages.
And therefore, if you wanna share your project code
with other developers,
it is often useful to delete node_modules,
and let those developers run npm install on their systems
to install those packages on their systems
so that you can share a lean project
without all these dependencies.
That's why all the code snapshots you find in this course
also won't have the node_modules folder
because if you download these attachments,
you can simply run npm install
in the extracted code snapshots
and then this will install these dependencies
on your local machine
but the download simply will be smaller
and will not be bloated by all these packages.
And that was now quite a bit of talking
about packages and NodeJS projects,
and how you install those packages.
But this is a vital aspect of building NodeJS applications,
and hence, it is important to understand that.
With Express installed though,
we can now continue
and we can now start writing some Express.js code
and see how that makes our life a bit easier.