Last Updated: February 25, 2016
·
8.71K
· tralamazza

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

2 Responses
Add your response

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 ·

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 ·