npmamazon-ec2next.jspm2

Can I use pm2 to keep a Next.js app running


I have an AWS EC2 that is just for keeping my Next.js client side running (there is no backend for it right now). Currently if I go into the terminal and enter npm run develop, the site runs perfectly. However, I want this process to always be running in the background of the instance. Is there a way I can do that with pm2?

I have it installed globally, but it won't let me run pm2 start npm run develop. BTW yes I want it running in development mode right now.


Solution

  • yes, but make sure the command exists in the package.json

      "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start",
        "lint": "next lint"
      }
    

    so in this case, the pm2 command will be

    pm2 start npm --  dev
    #in your case
    pm2 start npm --  develop
    

    enter image description here

    Or to have a nice name as mentioned by @brc

    pm2 start npm --name "next-js" -- dev
    

    enter image description here

    While running on EC2, it also good practice to save process for startup

    PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.

    https://pm2.keymetrics.io/docs/usage/startup/

    pm2 startup
    pm2 save