Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between local and global module in Node.js? When to use local and global module?

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.

like image 434
Badal Avatar asked Apr 09 '15 05:04

Badal


People also ask

Should I install npm globally or locally?

It's best to install locally when relying on a package from your module, such as Node. js. This is how npm install works by default. The grunt CLI package, for example, must be installed globally before it can be used as a command-line tool.

What is the main difference between local installation and global installation of packages with npm?

local packages are installed in the directory where you run npm install <package-name> , and they are put in the node_modules folder under this directory. global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>

Should I install node modules globally?

A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it's reused across projects. You can also install executable commands locally and run them using npx, but some packages are just better installed globally.

What is the difference between the global installation of dependencies and local installation of dependencies?

Installing the local dependencies means the module will be available only for a project you installed in the same directory. Global installing dependencies puts the module into your Node.


2 Answers

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.
like image 181
Tomasz Racia Avatar answered Oct 13 '22 20:10

Tomasz Racia


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

like image 31
Dinesh Agrawal Avatar answered Oct 13 '22 19:10

Dinesh Agrawal