Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should i use npm with "-g" flag and why?

Tags:

npm

People also ask

When should I use npm?

npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently. It is extremely configurable to support a wide variety of use cases. Most commonly, it is used to publish, discover, install, and develop node programs.

What is the G flag for terminal?

the -g flag means install the package globally on your system.

What is G in command line?

The G (go) command moves to a specified line and displays it. It can move both backward and forward from the current line. The G command displays one line at a time. [G][n]

Where does npm install G?

npm install -g pm2 - pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules (Use npm root -g to check where.) If you're using nvm, then your global modules may be in one of several places depending on the version of node you're using at the time.


-g is the global install flag, as explained in this answer. It's covered in detail in this node blog post.

The rules of thumb:

  • Install globally if the package provides command-line tools
  • Install locally if you're using the package as part of your application
  • Install globally and locally if both use-cases apply

While the accepted answer is correct, be aware that there is also npx which allows to conveniently run local tools.

For more information, see https://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner