npm
installs packages locally, ie. in a node_modules
folder inside your current folder. This allows your application to depend on specific packages versions, without having to mess up with a global list of installed packages on your system. See the first paragraph of Isaac's blog post (Handle multiple versions of the same thing at the same time), which explains well how npm
avoids the dependency hell often encountered in other programming ecosystems.
On the other hand, some packages are meant to be used as command line utilities, such as grunt-cli
, mocha
or json
. In order to use them everywhere, you need to install them globally, hence the -g
parameter.
Please note that you shouldn't need sudo
to install global packages, see this relevant answer for more information.