npm install vs sudo npm install -g

javascript, node.js, npm, sudo

Solution

`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.

Problem

for some packages I have to run `sudo npm install -g` while for others `npm install` will suffice. Why and what's the difference? For example: ``` npm install -g grunt-cli # doesn't work sudo npm install -g grunt-cli # works npm install websocket-stream # works ``` Is `sudo` necessary only with the `-g` flag?

Original source

Related problems