Node.js Lesson 5: Global modules

Node.js Lesson 5: Global modules

Node.js Lesson 5: Global modules

Hello everyone, we will learn about global modules in this lesson. We will talk about what are they, how they work, and why we need them. Let’s start.

What are global modules

Global modules are node packages that are installed on your system rather than your project directory. They allow us to use the package as a tool anywhere on the local computer.

By saying global, we are talking about the scope of usage of these modules. Generally, modules are scoped inside the project directory only, it means you can’t use them outside the project. But as global modules are installed on the computer (mostly root location), they can easily be used anywhere in our system. This property of node modules can come very handy in certain situations. We will talk about those situations later in this article.

How to install a module globally

To make a certain package available globally, we will use the below command:

This -g will make sure the package is installed in the system directory and thus it’s available globally. The installation of a global module is a little different from the normal one. Let’s see how.

Where do they get installed

Global modules are installed in the standard system directory /usr/local/lib/node_modules unlike generic installation which installs the module in the project directory.

If you want to know what’s the root location of the global module installation, you can do so by typing this command:

This will print the location on your system where all the global modules are installed. So now we know how to install them and where they get installed. Let’s talk about why we need them.

Why we need global modules

Globally means publicly available here. There are few things that we might need to do but we certainly don’t need those package inside our project. An example could a cli tool to create a project or a tool to format the code or it can be a cli tool to generate a file.

Remember when we typed npm init and it generated a packages.json for us. That’s a classic example of how we might use a globally available module. Some other examples could be create-react-app to create a react project, react-native-cli to create a react native project, and readme-md-generator to generate a README.md file for your

Consider these global modules as tools for your development. It makes our job easier.

How to check which packages are installed globally

To know the packages installed on your computer, simply type this command in the terminal:

It will give you the list of packages that are available globally.

That’s it for this lesson. We learned a lot of cool stuff about global modules. In the next lesson, we will talk about some most frequently used node modules and learn how to use them. Stay tuned for the next article.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Node.js Lesson 7: Console Module

This lesson will talk about another module which is by far the most commonly used module in node.js javascript environment. We will learn all the ...

Node.js Lesson 6: Util and Inheritance

This lesson and the coming one will cover some of the modules that we generally use in development. One of the modules is util and it comes very ...

Node.js Lesson 4: NPM Package Structure

Hey everyone, this lesson is going to be all about the node package and its structure. We will understand what the package.json file actually is and ...

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy