When you do npm install -g <package>”
then it means you want to install that particular npm module system wide (globally). And when you do without the g option, then module would be installed locally inside the current directory called node_modules.
The -g(global) directive tells npm
to install the package in the global shared node_modules folder and usually you get that where nodejs
resides(Path: C:\Program Files\nodejs).This will also allow you to access the module from the command-line at global level.
You can also check where global packages/libraries are installed:
Run below command
npm list -g or npm root -g
In windows:
C:\Users\admin>npm list -g
C:\Users\admin\AppData\Roaming\npm\node_modules
In mac: you find node_modules here
/usr/local/lib/node_modules
Whenever you put -g then it installed that file globally and that can accessed in any folder you wants then you don't need to install again again same package.
For more details.
You can also check where local packages/libraries are installed:
run npm list
or npm root
And, when you simply install then that particular package restricted to that folder.
So, whenever you package dependencies at global level then you need to put -g while installing folder.So,
- If you’re installing something that you want to use in your program,
using require('whatever'), then install it locally, at the root of
your project.
- If you’re installing something that you want to use in your shell, on
the command line or something, install it globally, so that its
binaries end up in your PATH environment variable.
check this.
Hope it helps.Thanks