Header Ads

How to Install NodeJS Modules Globally

If you ever wonder what a headache it is to install NodeJS modules to your designated location. Then you have come to right place. Installing NodeJS to the choice of drive is simple but installing NodeJS modules globally for multiple projects is a separate issue.

Today, I shall be demonstrating installation of NodeJS modules globally for Windows 10/11 machine.

 


Prerequisites:

Before proceeding any further in this article, following are some of the many prerequisites for this article:
  1. Installation of NodeJS.
  2. Knowledge of NodeJS.
  3. Knowledge of Windows Command Prompt.

The running working solution is provided. 

Download Now!

Let's begin now.

1) First you need to download and install the latest version of NodeJS on your windows machine. I recommend installing it in Drive D or any other drive other than drive C. The reason is to save the memory of Drive C in a long run. I have installed mine at "D:\Program Files\nodejs" path.



 
2) Now next step is to open Windows Command Prompt as administrator.


 
 
3) The first thing to do within Windows Command prompt is to change NodeJS default global modules installation and cache paths with the help of below commands i.e.

    npm config --global set prefix "D:\Program Files\nodejs\npm_pkgs"
    npm config --global set cache "D:\Program Files\nodejs\npm-cache"


By default NodeJS on windows machine install the global modules within the User profile's path. So, in a long run this will bulk up your drive C, which we do not want.

 

4) Next, use below command to install "moment" module globally for NodeJS i.e.

npm install moment -g


The module "Moment" provides rich features to manipulate date/time formats.

5) Once the module has been installed globally, Create your sample code in a file and name it index.js.

 

6) Then open windows command prompt again. You can open it without administration privileges. Then type below command first in order to create the link of the globally installed module within your targeted project i.e.

npm link moment


7) Now, type below command to execute the code and you will be able to see following i.e.

node index.js



Conclusion

In this article, you will learn to install NodeJS modules globally on windows 10/11 machine. You will also learn to change the global modules installation paths of NodeJS in order to save the memory of drive C and finally, you will learn to link the globally installed module with your project in order to run your code.


Video Demo