19

We can access local module using require function but cannot access global module through it. I read somewhere that to use global module we need to make it local then import it through require function. So if we cannot access global module directly, then what is the need of using it.

Badal
  • 376
  • 4
  • 11

3 Answers3

29

You should:

  • Install a module locally if you're going to require() it.
  • Install a module globally if you're going to run it on the command line.
Tomasz Racia
  • 1,786
  • 2
  • 15
  • 23
  • 1
    Thanks for your reply. Can you give a small example of running globally installed module through command line. – Badal Apr 09 '15 at 11:30
  • 1
    Personally I'm using `nodemon` installed as a global module (`npm install -g nodemon`) to run the server and automatically restart it when source code changes (perfect for development). So in this case instead of e.g. `node server.js` I execute `nodemon server.js` ;) – Tomasz Racia Apr 09 '15 at 11:37
  • 1
    Thanks again for your reply tomrac. But this is a specific use of global module. Can you please tell me the general use of it ? – Badal Apr 09 '15 at 11:54
  • 4
    I think good explanation you can find here: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/. When it comes to me, I always use local modules unless I want to use it in shell or on the command line. Then I don't have problems with versioning :) – Tomasz Racia Apr 09 '15 at 12:04
  • 2
    Thank you tomrac. This link really helped me to understand the concept. :) – Badal May 22 '15 at 09:27
4

I think in my opinion the modules which you are going to require in your code must be in local to your project or you can say must be present in your node_modules directory

and the modules which works as command must be installed globally. examples are exress-generator,jsdocs,mocha

Dinesh Agrawal
  • 326
  • 1
  • 3
1

A general use of global node module to my experience will be:

If all my applications that uses that same node module (of the same version or I do not care which version , latest is fine for me), then I will install that node-module globally. One good example.example will be node module that I use it for testing -- e.g. mocha.

hohoho
  • 220
  • 1
  • 3
  • 12
  • 1
    Is there any way to access the global node-module. If No, then there is no reason of installing it. If Yes, then how I will use it ? – Badal Apr 16 '15 at 04:47
  • 1
    Of course you can access it, otherwise why bother installing them, I think tomarc already explains some usages. – hohoho Apr 17 '15 at 22:36