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

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

For more information, see Introducing npx: an npm package runner


-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

Tags:

Npm