Sometimes, you may see the npm install
command paired with the -g
flag as shown below:
npm install -g
the -g
flag is a shorthand for the global
configuration which sets the package install location to the folder where you installed NodeJS.
This is useful when you need to run the package from the command line instead of using require()
and import it to your code.
One example of a package that you want to install globally is the Vue CLI
package that allows you to instantly bootstrap a VueJS project from the command line.
When you install without the -g
flag, the Node module will be installed inside the current folder where you run the npm install
command. You can’t call the module from the command line
When installing with the -g
flag, the Node module will be installed on your NodeJS global lib
folder. For example, when I install Vue CLI, the location of the install is /usr/local/lib/node_modules
npm install -g @vue/cli
# or
npm install --global @vue/cli
Once you install the CLI tool globally, you can call it from anywhere inside the command line to spin VueJS projects.
This is useful because some npm modules are CLI tools. They are going to be used from the command line and not from your application.