No comments yet

How to use node.js global modules locally

[Social9_Share type="horizontal"]

How to use Node js global modules locally?

When you are working in node js , you would have installed multiple modules globally using npm package manager. Now, what if you want to use the same module locally for few projects.

Is it necessary to install the same module locally for each and every project ?

Not required. Though, it is better to have the own copies of the module for every project, there are two ways where you can actually link the globally installed module to your local project. Let’s see that now. I have done this in Windows environment.

Using link:

First install the module globally using -g flag. I am installing express globally using

npm install -g express

npm-global

Then go to your project folder and type npm link <module-name>. This is applicable for Node with version > 1.0. For us, it is

npm link express

npm-link

Automatically, express module gets linked to the local project. You can see the shortcut of express folder now in your project.

npm-shortcut

So now whenever, you are updating it is enough that you update the global copy.

Using NODE_PATH:

What if you are working in a platform where link is not working for you or you are using lower version of node?
Then, you can set NODE_PATH (to the place where the module is installed globally) in environment variables.Usually it is in C:\Users\{username}\AppData\Roaming\npm\node_modules. . Let me set this in the environment variables.

To do this,

  • Select My Computer right click -> Properties.A new window opens.
  • Select Advanced system settings.Another window opens.
  • Select Environment Variables. Again a new window opens.
  • Click New. A window opens.
  • In Variable name type NODE_PATH.
  • In Variable value give the path as C:\Users\{username}\AppData\Roaming\npm\node_modules .
  • Click OK, for all windows prompt and come out.

The below picture shows it clearly.

node-path

Once you do this, while working in your local projects, the system will automatically go and search for the modules in this folder. Now, let us check this.

Let me create a folder Demos in D:\ drive and create app.js file in this folder. This file is looking in for a module called express. The javascript file is given below.

appjs

Now, let us go back to console and try running this file as

node app.js

node-run

You can see that this is working fine, listening to port 3000. Let’s check the folder.

app-folder

If you notice the node_modules folder is not created. It means node can still go and get the modules that are sitting globally because of the path you have set.

Global or Local which one to choose?

If you want to have modules specific to your project, it is better to install the modules locally.
If you have a bunch of projects using the same module(like express) you can think of installing them globally.
But always, installing them locally is a better choice, as you won’t mess with the global module, while working in different projects using the same module.

That’s all.
Happy Learning.

Post a comment