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.
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.
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>
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.
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.
You should:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With