[MUSIC]
>> Hi, everyone. My name is Yohan.
I'm a Senior Cloud Developer Advocate working at Microsoft.
In this video, we'll talk about Packages,
what they are, why they're important and how to use them.
But first, what's a Package?
A Package is a re-usable bundle of code that
may also include other things like
images, fonts or any resources.
Packages can be many things like
libraries that you use to build your applications.
For example, React, Angular or Express.
They can also be tools like the TypeScript compiler,
Unit Test Runner or [inaudible].
You'll probably use a lot of
third-party Packages to build your application.
At some point, you may even create
your own private Packages to share
some components if you have multiple applications.
Ultimately, even though application you're creating is a Package.
You may also hear sometimes
the words dependencies or
modules when going through documentation.
Maybe confusing at first, but it's certainly different worlds
well meaning Packages at the end.
You might wonder now,
"Where can I find Packages to use in my project?"
In the JavaScript ecosystem,
there's multiple places where you can find
Packages but the most popular one is npm.
There's more than one million Packages on
npm and that number is growing every day.
It's both a registry,
meaning a place where you can share under
note Packages from and also a CLI too.
The npm CLI comes with every NodeJS installation
and that's what we'll use to manage Packages in our project.
Now, why are Packages important?
If you look at this code here,
this is a fully functional web server
built with less than 10 line of JavaScript.
Think about it for a second.
If we did not use a Package here,
we would have to write way more code than
that to handle the routing of the HTTP requests,
to create the HTTP response,
to handle the formatting of the headers,
the transformation of the data and a lot of
other complex and boring tasks.
Thanks to the Express Package and
this single line of code we have here at the beginning,
you don't have to code all of that.
You can focus on what really matters and be way more productive.
To be able to use Packages in your application,
it all starts with a filename, package.json.
This is a JSON file that consists
of mainly three groups of information.
You'll find in there a lot of metadata,
like your project's name, version, license,
and some other information.
Then you'll find dependency section.
This is the list of the Packages that you use in your project.
Said otherwise, this is the Packages your project depends on.
Finally, you have a script section.
This is where you can put commands to
automate your development tasks like,
for example, compiling your code or running your test.
To create this file, you can use the npm CLI init command.
It will ask you a bunch of question and it
will create the package.json file for you.
If you want to start quickly,
you can even skip the prompts and fill in
the fields later using the -y option.
Once you have the package.json file,
you can start installing Packages.
For that, you have to use the command npm install,
followed by the name of the Package you want to install.
If you add the option - -dev to this command,
the Package will be added to a different section of your
Package.json file called devDependencies instead of dependencies.
But what's the difference between the two?
In your project, you'll find two kinds of Packages.
There will be Packages that your application
needs in order to be able to run in production.
This Packages will be your dependencies.
There are also Packages that you need
during the development of your app to help you,
like there's libraries, code formatters or compiler tools.
But these are only useful during development.
You don't want to ship their code in
production because it's useless.
As these Packages are only needed for
development that's why they are called devDependencies.
In the next video, let's put all of that in
practice and create a new project that use Packages.