I'm getting errors with
sudo npm install <package>
npm as a super user has a risk of running some untrusted code as a super user which can potentially mess with your entire system. Running
npm as an unprivileged user has a risk of running that code with less privileges and it won't be able to mess with the entire system - just with your own files (which can be equally bad, depending on how you look at it).
What I often do and recommend is to install Node in your home directory instead of globally on the system if it's your own computer. That way you don't have to run with
npm or even for
make install of Node itself.
I run a lot of versions of Node that I compile from sources sometimes with different switches and the convention that I use is to install Node in versioned directories, either globally in
/opt (but then you need
sudo) or locally in my home directory in
I do it like this:
wget https://nodejs.org/dist/v7.1.0/node-v7.1.0.tar.gz tar xzvf node-v7.1.0.tar.gz cd node-v7.1.0 ./configure --prefix=$HOME/opt/node-v7.1.0 make && make test && make install
Then I create a symlink
~/opt/node pointing to
~/opt/node-v7.1.0 and I have:
That way I don't have to run as super user for installing Node or for running npm.
As a bonus I can quickly switch my default Node version just by changing the symlink, and at any time I can run any other version if I change the PATH or run Node with a full path like
I explained that installation process in more detail in my other answers:
I don't want to go into too much detail here since this answer is about why running
npm as a superuser is not a good idea - this installation process is just one solution to not have to run
npm as a superuser.