Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm install vs sudo npm install -g

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?

like image 910
Connor Leech Avatar asked Nov 28 '13 10:11

Connor Leech


Video Answer


1 Answers

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.

like image 106
Paul Mougel Avatar answered Sep 22 '22 21:09

Paul Mougel