02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Installing and Uninstalling Global Packages with NPM

Installing and Uninstalling Global Packages with NPM

In this tutorial, we'll walk you through installing and uninstalling global packages using NPM (Node Package Manager). Global packages are essential when you need tools available across your entire system, such as linters, task runners, or other command-line utilities. This tutorial was originally created in 2016, and any important updates are included to keep the content current.

Step 1: Installing Global Packages

Global packages are installed across your entire system and can be accessed from any directory. To install a package globally, use the -g flag with the npm install command.

Example: Installing a Global Package

To install a package globally, run the following command:

npm install -g package-name

For instance, to install nodemon, a tool that automatically restarts your Node.js server when file changes are detected, use:

npm install -g nodemon

After installation, you can use nodemon from any terminal session.

Critical Update (2024): It is recommended to use npx instead of globally installing packages, as this avoids potential version conflicts and reduces the number of globally installed dependencies.

Step 2: Listing Installed Global Packages

To see a list of all the global packages installed on your system, you can use the npm list command with the -g flag:

npm list -g --depth=0

The --depth=0 option limits the output to only top-level packages, making it easier to see which packages are installed globally without listing all their dependencies.

Step 3: Uninstalling Global Packages

If you no longer need a global package, you can uninstall it to free up system resources and avoid clutter.

Example: Uninstalling a Global Package

To uninstall a global package, use the following command:

npm uninstall -g package-name

For example, if you want to uninstall nodemon, you would run:

npm uninstall -g nodemon

This command will remove nodemon from your global packages, and it will no longer be accessible from any directory.

Step 4: Understanding When to Use Global Packages

Global packages are ideal for command-line tools that you want to be able to use anywhere on your system. However, for libraries or dependencies specific to a project, it's better to install them locally within that project's directory. Installing packages locally ensures that different projects can use different versions of the same package without causing conflicts.

Tip: Use global installations sparingly to avoid version conflicts. If you need a package for a specific project, always install it locally using npm install package-name without the -g flag.

Conclusion

In this tutorial, you learned how to install and uninstall global packages using NPM. Understanding how to use global packages can significantly enhance your workflow, especially when using command-line tools that are needed across multiple projects.

Remember that while global packages are useful, it's essential to manage them wisely to avoid version conflicts. Where possible, consider using npx or installing packages locally within each project to keep your environment clean and avoid dependency issues.

Video Transcript

00:00Welcome back.
00:01Now that you have NPM installed
00:03and all of its permissions configured, it's time for us
00:06to learn about the first thing you could do with NPM,
00:10which is installing global packages.
00:12Through this lecture, you're gonna learn
00:14what the global scope is.
00:15You're gonna learn how to install into it packages
00:18and how to remove from it packages.
00:21So let's get started. Let's see how that's done.
00:24In a previous lecture, we talked about
00:27the different locations where the files are globally,
00:31and I wanted to do a quick recap as we move onwards.
00:34If you recall, we said that if we ask from NPM
00:37to reach its config file
00:41and get our prefix,
00:43and the prefix is the base folder where NPM really starts
00:47to gather information.
00:49Then you can see that it's the base folder is at
00:52user slash local.
00:53And if we actually go into that folder,
00:55but go a specific folder actually
00:58where packages get installed inside of the user local,
01:02there's a fold folder called Lib,
01:05and in there there's another folder called node modules.
01:10Now if you go inside of that folder
01:12and just do a quick lookie inside of it with ls, you'll see
01:16that there are already three packages installed by default.
01:20In this current build of NPM, we have gulp,
01:23and by the way, this could have been also part
01:25of the installation with no js.
01:28So we have your gulp gulp js lin,
01:31and we have NPM, which is our NPM itself.
01:35Now we, there's another way of approaching this.
01:37I'm gonna go ahead and just like move out
01:39of this folder into a directory that we're gonna use
01:42for our local installations.
01:44So it's gonna be just a package that I have here.
01:46So lemme just clear the view.
01:47It's just a local folder that has nothing
01:49inside of it currently.
01:51Now what I want to do is I want to approach NPM
01:54and I want to ask from NPM directly
01:57to list out the packages that it has.
02:00And if I do N-P-M-L-S, we're gonna get a nice long list.
02:03Well, a nice long list of nothing
02:05because currently in the folder that we're in,
02:08there is no packages.
02:10So there are no packages, there are no dependencies,
02:13there are no nodes, there are no,
02:15nothing really inside of here.
02:17But if I go ahead and change the path
02:19to LS minus G basically approaching the global,
02:23then I'm gonna get a nice long list
02:25of all the different things that are installed
02:28and notice the dependencies that are coming within.
02:31I don't really care about the dependencies right now,
02:33so I'm gonna go ahead and do the exact same command again,
02:37but this time I'm also gonna set a depth, a depth of zero.
02:42Once I do that, I'm gonna get only the top layer packages,
02:45which are those three packages or NPM, gulp and Gulp JS Lin.
02:51Beautiful. So now you know how
02:52to list out both local packages and global packages.
02:57When we're saying global, we mean
02:59Every single application that we have has access
03:02to those packages while local are stored directly inside
03:06of a specific folder, a local folder that you have.
03:11So in our case, you're really talking about, um,
03:14opening it up inside of basically a folder.
03:17Right now, this is the folder that we're in that has nothing
03:20inside of it, but we're gonna take a look at
03:22that in a mo in a moment.
03:24What I wanna show you is different steps involved in
03:26what could you do with packages.
03:28There are three main things you could update.
03:31You could uninstall and you could install of course.
03:33So what, let's go ahead
03:35and ask from NPM to update a package.
03:38Now we only have three packages
03:40and right now we wanna approach the global packages.
03:43So I'm gonna set a minus G there
03:45and I'm gonna say that I want gulp,
03:47so I'm just gonna type in NPM update minus g gulp.
03:51Once I hit enter
03:52and I let it run, what's gonna happen is gonna search within
03:55the repository
03:57to see is there an update from Gulp 3.90.
04:01And if there is, it's gonna go ahead and update it.
04:04Again, if you had any issues
04:06with your permissions previously, make sure
04:08that you update those
04:10and you could see that we've just updated our gulp from 3.9.
04:14What was it? Let's see.
04:16Let's see if we could get all the way up there.
04:17Woo, there's a lot of, a lot was added in. Here we go.
04:21Our gulp was 3.90
04:23and we've just updated it to 3.91.
04:26So we've just updated it a little bit.
04:30Now lemme go ahead and clear this again.
04:32And if I go ahead and list out our packages again,
04:34our global packages, we're gonna see
04:36that now we have your gulp 3 9 1 instead of 3 9 0.
04:41Um, my gulp js lint is at 0.22, which we could try
04:44and update it as well.
04:46But before we do that, I wanna show you
04:48how do you actually uninstall a package?
04:51And again, this, it's very similar.
04:52You just type NPM uninstall, you then set
04:57where do you want to uninstall something globally?
04:59And be careful because when you uninstall global things,
05:02there's a price to that 'cause you're affecting
05:04every single application.
05:05Same, it's true with updating.
05:07And I'm gonna just go ahead
05:08and I'm gonna delete, uh, gulp by telling NPM to uninstalled
05:13globe gulp globally.
05:16So went through and got rid of our package
05:19and if I just go ahead here and clear our stage
05:22and just asks to get a list of all the packages
05:24that we have, you'll see that now we're missing gulp.
05:28Now there's only two packages.
05:30And last but not least, we saw how to uninstall.
05:32We saw how to update.
05:34Let's see, also, how do we do an NPM install?
05:38Global gulp, literally same, same applies.
05:41So I, we went through the process of updating uninstalling
05:45and installing.
05:47And if I go ahead here and I, uh, run it just one more time.
05:50This time instead of doing an update, it's gonna go ahead
05:52and fetch a, a fresh new copy of gulp. As it fetches
05:57That that copy, it's gonna put it into the global scope
05:59because that is the scope that we requested.
06:02Now there's other packages that we could, um,
06:06that we could, we could work on.
06:08And it doesn't have to be on the global scope.
06:10So right now we, 'cause we deleted it,
06:12it's gonna take us a little bit of time for it to go
06:15through the process of the installation.
06:18In this lecture, we've learned about the global scope
06:21and installed and removed packages from it.
06:24In the next lecture, we'll move from the global into the
06:27local and learn about the differences of installing
06:30to the global scope versus the local one.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!