lately I’ve been ask a lot about

node.js -g command

and what does it do.

It is actually referring to npm -g command which is used to install node.js modules globally,

this means that the module is accessible from anywhere in a command line window (CMD) regardless of the current directory we reside in.

To install a npm package or node.js module globally we can use the following:

  $ npm install -g package-name

It is important to understand that the -g usually used for when the node.js module or package is used through command-line utilities

to put the module in bins in the system PATH, where you can execute a specific node.js command through command-line utilities

and it is not used for require('./module') function.

for example to install the popular simple http server module globally we can use:

$ npm install -g http-server

npm install -g

npm install -g

once this module is installed, you can navigate to any folder, typically a website content folder, and run http-server command

and server the content of the folder to your local network.

http-server

http-server

 

I usually use this node.js module to test my website development and/or share files between different devices through Wi-Fi

If you want to learn more about the node.js, please check out my simple node.js tutorial here.

more information on npm global:

https://docs.npmjs.com/misc/faq#how-do-i-install-something-on-my-computer-in-a-central-location