dxmfpg
Last Updated: February 25, 2016
·
7.919K
· tralamazza
781bbe3e6880c14f783d044f2ff05e50

One does not simply sudo npm

Let's say you want to install forever globally:

sudo npm install -g forever

The problem here is, if you do not have a ~/.npm (npm's cache folder) and you sudo it, the cache folder will get root:root permissions. So the next non sudo install will fail with:

npm ERR! Error: EACCES

This happens because by default (on linux at least), sudo does not set $HOME (i.e. impersonates) to the target user (which is root by default).

To "fix" that you should use the -H option.

sudo -H npm install -g forever

ps: Most people won't ever notice this problem because if ~/.npm already exists npm won't overwrite its permission.

ps2: If you must use forever under sudo, use it with -H sudo -H forever list

Say Thanks
Respond

2 Responses
Add your response

11143
127a3daff3587eb8d970733aa4dd23d0

You shouldn't need to sudo any npm install -g. Install node and npm under the current user and don't use sudo at all.

over 1 year ago ·
14984
81e0c733d0b537cf8b7fcc315e78d6e2

Oh man, it took me a long time to find this page, saved my life. I have an automated process running under supervisor where npm install wasn't working because $HOME was pointing to the root's home.

over 1 year ago ·