React build run on server using pm2

RDoonds picture RDoonds · May 2, 2018 · Viewed 26.2k times · Source

I have compiled my react app using

react-scripts build

And it generated a build\ folder in root directory of App. I am running the build\ folder using

sudo serve -T -p 443 build/

This runs my React app successfully on https since I am passing -T. But I needed to run my app forever using any of the modules available . I was looking into node modules forever & pm2 I am trying to using pm2 in following way:

sudo pm2 serve -T -p 443 build/
It throws:
error: unknown option `-T'

and when I use:

sudo pm2 serve -p 443 build/
It works on console but I am not able to access my app from URL

Can someone help me with this? Or if there is any other way to run your react app on production forever.

Thanks in advance

Answer

bgran picture bgran · May 2, 2018

You need to use a pm2 JSON config to run arbitrary binaries:

app.config.json

{
  apps : [
    {
      name      : "your-app",
      script    : "npx",
      interpreter: "none",
      args: "serve -p 8443 -T"
    }
  ]
}

To start:

pm2 start app.config.json

interpreter: "none" tells pm2 not to treat the script like a JavaScript file when executing, and instead to treat it like an ordinary binary.

If you have a serve binary in the same directory as the app config, you can execute serve directly instead of npx.