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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With