Sunday, May 1, 2016

pm2 must not be ran with sudo

If you got an EACCES error when you run pm2 without sudo..

me@ubuntu:~/myapp$ pm2 start ./bin/www
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: connect EACCES /home/me/.pm2/rpc.sock
    at Object.exports._errnoException (util.js:896:11)
    at exports._exceptionWithHostPort (util.js:919:20)
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1073:14)


..chances are you might try to run pm2 with sudo and it will work.

However, it will cause an undefined value on process.env.PWD, and then it will cause SPA page that are refreshed or re-loaded from bookmark not to load.

var base = process.env.PWD;

res.sendFile('index.html', {root: path.join(base, '/public')});


To fix the error, make the following your own file /home/yourUsernameHere/.pm2/rpc.sock

sudo chown yourUsernameHere ~/.pm2/rpc.sock


Doing the above you can now run your pm2 without the sudo.

After running:

$ pm2 start ./bin/www

Run this:

$ pm2 save
$ pm2 startup

Then follow the instruction of that pm2 startup command.


Happy Coding!