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 17: Timers, Differences from Browser, ref and ref

Hey, guys! Our today’s article will tell you about Node.js timers. Here we will try to tell you about all the differences existing between browser ...

Nodejs Lesson 16: Internals of Nodejs: Event Loop

This lesson will continue the last lesson where we learned how the LibUV library works internally. In this lesson, we will learn about Event Loop and ...

Nodejs Lesson 15: Internals of Nodejs: LibUV

Hello everyone, today we are going to talk about the internals of Nodejs. This article will guide how node js works and how it can handle async ...

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