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

Understanding Global and Local Packages in NPM

Understanding Global and Local Packages in NPM

In this tutorial, we will dive into the differences between global and local packages in NPM (Node Package Manager). Understanding when to use global versus local packages is an essential part of managing your Node.js projects effectively. This tutorial was originally created in 2016, and we've included any critical updates to keep the information relevant for today's development environment.

What are Global and Local Packages?

Global Packages

Global packages are installed across your entire system and are accessible from any directory. These packages are typically used for command-line tools that you need to use globally, regardless of the current project directory.

  • Installation Command: Use the -g flag with npm install to install a package globally:

    npm install -g package-name
    
  • Example: Installing nodemon, a tool that automatically restarts your Node.js application when files change:

    npm install -g nodemon
    

Once installed globally, nodemon can be used from anywhere in your terminal.

Critical Update (2024): It is recommended to use npx for one-time usage of command-line tools instead of globally installing them. This reduces the risk of version conflicts and keeps your environment cleaner.

Local Packages

Local packages are installed within a specific project directory and are only accessible in that project. These packages are defined as dependencies in the project’s package.json file.

  • Installation Command: To install a package locally, use npm install without the -g flag:

    npm install package-name
    
  • Example: Installing express for use in a specific project:

    npm install express
    

This will create a node_modules folder in your project directory, containing the express package and any dependencies it requires.

When to Use Global vs. Local Packages

  • Global Packages: Use when you need a tool available across your entire system, like linters or build tools (e.g., nodemon, gulp, or http-server).
  • Local Packages: Use when the package is a library or dependency required only for a specific project. This ensures that different projects can use different versions of the same package without conflict.

Tip: As a best practice, limit the use of global packages and instead use npx or install locally whenever possible. This helps avoid issues with mismatched versions across projects.

How to List Installed Packages

Listing Global Packages

To list all the globally installed packages, use the following command:

npm list -g --depth=0

This command will show you all the global packages installed on your system without showing all their dependencies.

Listing Local Packages

To list the locally installed packages for a project, navigate to the project’s directory and run:

npm list --depth=0

This will list only the top-level packages installed in the current project directory.

Removing Packages

Uninstalling a Global Package

To remove a global package, use the -g flag with the npm uninstall command:

npm uninstall -g package-name

Uninstalling a Local Package

To remove a local package from your project, use the command without the -g flag:

npm uninstall package-name

This will also remove the package from the package.json file if it was listed as a dependency.

Conclusion

Understanding the differences between global and local packages in NPM is crucial for managing dependencies efficiently in your Node.js projects. Global packages are useful for command-line tools that need to be accessible from anywhere, while local packages keep project-specific dependencies isolated, ensuring compatibility across different projects.

Remember to be mindful of when to use global versus local installations. Where possible, use npx to execute commands without globally installing packages and always install project dependencies locally to maintain consistency and avoid conflicts.

Video Transcript

00:00In the last lecture, we learned about the global scope
00:03and how to work with it.
00:04And in this one we cross over from the
00:07global end to the local.
00:08And with it we're gonna answer
00:10and really understand what is the global scope,
00:13what is the local one,
00:15and how do we update packages in general?
00:17So let's jump right into it
00:19and let's explore the differences between global and local.
00:24Alright, our installation
00:26of gulp completed into the global scope.
00:29And again, the way we approach
00:31that global scope is we would type,
00:33and let me just clear our screen.
00:35NPM ls, we want global, uh, we don't want to very deep,
00:40so we just want to know on the surface
00:43what are the things that were installed.
00:45And you can see the list of the packages
00:47that are installed once it fetches it and gets it back,
00:50and you can see that now we have gulp version 1.0 0.9.
00:55Now, the next step that I want
00:57to do is I wanna show you the process of
01:00how do you globally update all of the packages.
01:04The way you do that is NPM update global,
01:09and then you don't specify anything.
01:11And once you do that, it's gonna go
01:13through the whole process
01:15and search for, to find what needs
01:17to be updated in the global scope.
01:20It will fetch, it will figure it out,
01:22and it's gonna make all the updates that are necessary
01:24for all the different packages that you have.
01:28The same is true, by the way.
01:29If you're working with a local folder only, you could see
01:32that gulp js lint has been updated as well.
01:36Um, and if I just go ahead here
01:38and get that list one more time, we'll see
01:40that we moved up our gulp js lint
01:42to version 1.01.
01:47Now last but not least, now that we've got all of this done
01:50and configured, um,
01:51let's start talking about the local folder.
01:54I mentioned that we're working on a local folder right now,
01:57which is called our NPM folder,
01:59which you could find in our source files.
02:02Now, if you go ahead
02:03and really don't need our source files for any
02:05of these samples, just create an empty folder
02:07and go to that empty folder.
02:10This time around when you're gonna install a package,
02:13don't list global, just go ahead and click on install.
02:17And this time just mention what you wanna install.
02:20For example, I'm gonna go ahead and,
02:21and install a very simple package that has no dependencies,
02:25that is called is array.
02:27The is array package is used by node js to check if, uh,
02:31if something is an array or not.
02:34Um, you could use it for that, that dependency.
02:36And I'm gonna go ahead and install it
02:38and notice how I'm installing it locally.
02:41Don't worry about this error message right now
02:43because we're gonna take a look into
02:45that in the following lecture.
02:47But if you take a look at this directory, you'll see
02:49that now a new sub folder,
02:51a fold sub folder was created called Node modules.
02:55And you have your one folder.
02:57And in that folder you're gonna
02:58Have all the different, the, the script
03:00that is basically creating this file.
03:02So you can see that this module has basically a small little
03:06script here that checks to see if something is an object,
03:09a very silly, simple script that enables you then
03:11to have access to it.
03:13And if we look at their documentation,
03:15we'll see in the documentation that you basically,
03:18this is the, the configuration of how you set it up inside
03:21of node jazz, which we're gonna look at how to set this up
03:24and configure it in the next chapter.
03:27But in the meantime, we wanna still focus on setting up
03:30and preparing and configuring your NPM
03:32so you really know how to work with it.
03:34And you don't end up in a situation
03:36where you're just copy pasting
03:38and you don't really know what's actually going on.
03:41So that said, you know, everything you really need
03:43to know about installation, uninstall, installing, updating.
03:47Um, and by the way, just one more note, if we wanted
03:49to update and we wanted to update everything,
03:51then we just click your update without saying global,
03:54and we would update all of the local directories.
03:58One last note is if we set here Ls this time
04:00around without the global, then we could get a list
04:03of all the different packages that are in the folder.
04:06So all this stuff we've done for the global works in the
04:08same way with the local and the global.
04:10With that, with that exception that you set there,
04:13that minus G to tell NPM if you want
04:17to install it in the global directory
04:19or in the local project directory.
04:23In this lecture, we really wrapped up the, the topic
04:26of installing, updating and removing in the global
04:29and local spheres.
04:31In the next lecture, we'll move into a more advanced topic
04:34that pushes us towards no JS projects,
04:37where we'll declare NPM packages with the package.
04:41Do JSON file.

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!