1

I have created node application and I want to deploy it on AWS.

I have setup project and want to make node permanent so I have installed pm2 globally, but when I tried to start node app with pm2 with this command

sudo pm2 start index.js

It's giving me error:

sudo:pm2 is not a command

Without sudo I can't start server.

Styx
  • 9,863
  • 8
  • 43
  • 53
Kalpesh Kashyap
  • 824
  • 11
  • 18

1 Answers1

4

Apparently pm2, and other global modules aren't in your root path. You can pass the PATH variable like this:

sudo env PATH=$PATH pm2 start index.js

This should do what you want.

Though mainly I would suggest not doing this, as probably you wouldn't want your application to run with root user, rather it's own isolated and limited user so in case vulnerabilities, the damage would be limited.

If you want to run pm2 on your machine startup, then you can use pm2 startup command which will instruct you how to do this properly.

Also if you are running your application as root, so you can listen on port 80, then that's the wrong way. I suggest have a look at this question. There are some good suggestions such as using ip tables to forwad port 80 traffic to another port which wouldn't require your application running on root.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115