RDoonds
RDoonds

Reputation: 525

React build run on server using pm2

I have compiled my react app using

react-scripts build

And it generated a build\ folder in the 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 the 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

[ec2-user@ip-10-XXX-XX-XXX UI]$ sudo pm2 serve -p 443 build/
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/local/lib/node_modules/pm2/lib/API/Serve.js in fork_mode (1 instance)
[PM2] Done.
[PM2] Serving /var/www/html/UI/build on port 8080
┌─────────────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name                │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├─────────────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ static-page-server-8080 │ 0  │ fork │ 26609 │ online │ 0       │ 0s     │ 2%  │ 21.7 MB   │ root │ disabled │
└─────────────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

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

Upvotes: 17

Views: 74649

Answers (6)

Upendra Shah
Upendra Shah

Reputation: 2301

You can run react-scripts with PM2

pm2 start node --name my-app -- node_modules/react-scripts/scripts/start.js

It will run on 3000 port and if you want to set different port

For Windows:

Set PORT=5432 && pm2 start node --name my-app -- node_modules/react-scripts/scripts/start.js

For Linux/macOS:

PORT=5432 && pm2 start node --name my-app -- node_modules/react-scripts/scripts/start.js

Upvotes: 0

Pavan Dhamu
Pavan Dhamu

Reputation: 31

If you are willing to run React project using pm2 then try to run below command

pm2 start --name <app name<app name>> npm -- start

Upvotes: 3

JHM16
JHM16

Reputation: 798

Use below command it worked for me

first build your react application and then hit this command inside your application folder .

pm2 serve build/ 3000 --name "react-build" --spa

Upvotes: 24

Tanmay Shrivastava
Tanmay Shrivastava

Reputation: 579

create run.sh file

put below command inside run.sh file

serve -s build

and save.

Then run this command.

sudo pm2 start run.sh --name app-name

Upvotes: 2

Nditah
Nditah

Reputation: 1759

@bgran provided a nice solution. As an alternative, I dare to suggest you can add this deploy to your script in package.json

"deploy": "pm2 start ./server.sh --name yourAppName",

Then in the same directory as the package.json, create an executable server.sh:

echo "Serving yourAppName!"
serve -s build

Don't forget to make server.sh an executable by running:

chmod +x server.sh

Now it's party time! Deploy your app by running

npm run deploy

Done!

Upvotes: 3

bgran
bgran

Reputation: 899

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.

Upvotes: 27

Related Questions