Insall node and npm with no sudo
I’ve been using node
and his good buddy npm
for a couple of years now. Up until three days ago, I would happily prefix sudo
whenever an npm
package gave me an EACCESS
error. I’ve always known this is bad practice, but had never encountered an issue. This all changed when I attempted something fairly mundane: Deploy a Hexo blog with Capistrano.
For better or worse, my Capistrano deployment runs npm install
as part of its routine. I tried mucking around with sudo
and visudo
, but to no avail. The blog would simply not deploy because of the restrictive sudo npm install
step. At some point it finally occurred to me that npm
shouldn’t need sudo
anyway, so I’d best fix the problem properly. Instead I did a quick hack on my production server:
The quick hack
Just kidding, this is actually legit, especially given that I’m the only one able to muck around in production. The following is adapted from here. Basically, it allows you to use the -g
option without sudo
, because all global npm
packages get stored under your home directory:
|
|
Append the following to the ~/.profile
just opened (or created):
|
|
Now update your system variables:
|
|
Install something globally to make sure it works:
|
|
A nicer way, arguably
This is more appropriate for a production environment with multiple users. It is adapted from here.
Create a new group:
|
|
Add current user to the group (with logname
)
|
|
Get access to group:
|
|
You can check to see that the user has been added by running:
|
|
Change group ownership on all the critical components:
|
|
On existing installs
After fixing the issue, there’s a real good chance that root
still owns some of the packages installed in the user’s home directory. That’s easy to fix:
|
|