Thought leadership from the most innovative tech companies, all in one place.

How to Build React for Production Using PM2

Improve basic performance and reduce load time by hosting React app as a static build..

Reactjs with PM2

Follow the below steps:

1. Create a React app

(ignore 1st command if you have a project already created)

npx create-react-app react-app && cd react-app
2. Open the React project and run the command:
npm run build

This will create build/folder in your project.

3. Now install the PM2 tool

It helps to run the react.js app in the background.

sudo npm install -g pm2
4. Create a new file with the name app.config.json

Below the build folder and add the following snippet.

{
  apps: [
    {
      name: "react-app",
      script: "npx",
      interpreter: "none",
      args: "serve -s build -p 3000",
    },
  ];
}

Now we are ready, you can change React.js port number (3000) and name of the app (react-app) from the snippet based on your need.

5. Run the following pm2 command

Which will start the app on your local or cloud instance ( i.e AWS EC2 ).

pm2 start app.config.json && pm2 list

This command will start the app and show the list of the pm2 running processes. Finally static build is running now




Continue Learning