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.
-g
is global, without just installs the package locally.
You run it with sudo as it installs to folders which your default user may not have access to by default.
npm install -g grunt-cli
installs the package in the global mode, every user could use it.
Without -g
you just install it in the current directory.
If you are not the root user, you need to use sudo
for the -g
.
If you use npm
without -g
and you have write permission to the current directory, then
sudo
is not necessary. Otherwise, you still need it.
grunt-cli
will provide an executable that will be put in your PATH
, so depending on how you configured your system it will require root access.
See this post from npm creator, particularly the part about using sudo with npm.
websocket-stream
is a library, your code will use it so it will be easier to perform some tasks, usually it will be installed at the root of your project, in the node_modules
folder.
Looks like permissions issue. -g install it globally (you will need to 'root'), but its not a good idea to install that as root
In terminal run:
sudo chown -R `whoami` ~/.npm