[MUSIC]
>> Hello everyone, I'm Jen Looper, and in this video,
we will see how to work with Node.js dependencies.
When you work on an application,
you write code to implement business requirements,
but you and your team might not write all the code yourself.
You might rely on external code like libraries and
tools that someone else wrote called packages or dependencies.
To install a package in your Node.js project,
again, we're going to use the npm CLI,
for example, by typing in a terminal and a project that I
have open, npm install express.
I'm adding the express library to my Node.js app
and you can see that a node modules folder
has been created on my file system
and this is where the node packages are installed.
Also in my package.json file, you can see
that it was added under the dependencies property.
Now there's something that you should do in your Node.js
project and that is opening or editing
a gitignore file and add to that your node_modules folder.
Why did you do that? Well, once you
started having a lot of dependencies in your project,
this folder can grow up to many gigs of data and the packages,
and it will often change as you update them,
so it's really not a good idea to have
that right in your Git repo.
The second reason is that if a coworker checks out
the project by just typing npm install,
all the dependencies mentioned in the
package.json will be restored,
and so there's really no need to commit them.
Now, where do these packages all come
from and where can you find new packages?
There's something I didn't tell you,
and that is that npm stands for
Node Package Manager and it is not only a CLI tool,
but it's also a global registry of packages.
If you open up the npm website,
npmjs.com, you can search for any package by name.
For example, if you search for typescript,
you can see all the packages
associated with it with some insightful information,
like its popularity, its quality, and its maintenance.
This information is really useful when you're
trying to decide which library to use in your project.
By default, npm will use this public registry for packages,
but you can also install packages from other resources,
you could set up your own private registry
which might be useful for a company,
you can install packages directly from
a Git repository or even from your own file system.
Now let's install a tool for testing our app.
For that, we're going to use jest,
so in my package.json file,
I can watch for the dependencies that are being installed.
I'm going to type npm install
--save-dev jest and we can watch it being installed,
and you can see that jest was added under
the dev dependencies property instead of dependencies.
Why is that? Well, that's because
dependencies belong to one of two categories,
production and development dependencies.
Production dependencies are dependencies
that you need to run an application in production.
For example, a web framework like
express that we'll use to build our application
and development dependencies are
dependencies only needed for our development
like tooling, for example, test libraries,
linting tools, or compilers, and
these are things you don't want to ship in production.
Now, what if you don't need a package anymore?
You can remove a package with the command npm uninstall,
so I'm going to type npm uninstall jest.
Now, jest has been removed from both the node_
modules folder and our package.json file.
There's a lot more to cover about
packages like how to update them,
how to manage vulnerabilities,
and we're going to see that in our next video.