Serving React & Node.js application with PM2 as a service (Linux /MacOS)

Nihal Khan
2 min readMay 13, 2022

--

Actually there are many utilities to run a React JS application as a service, but I like the ease of PM2.

Without wasting any time lets start …

1. First install PM2 using any of these commands —

$ npm install pm2@latest -g
# or
$ yarn global add pm2

Preferably use option -g or global. You need to install it globally.

2. Once installation is done run below command -

$ pm2 list

List command list down all the applications running under PM2.

Since right now there is no running process under PM2, list is empty.

3. Now from command prompt navigate inside your project’s root folder and run below mentioned PM2 command.

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

I have a dummy dashboard react.js project and I want to run that as a service using PM2. So I run this command from root folder of my react.js project —

$ pm2 start --name dashboard npm -- start

Output:

You can see once I ran that command PM2 started a service with name “dashboard”. And status of the process is “online”, that means my application already running, I can quickly test that from browser using localhost:3000

You can use host name with the port application is served upon.

To stop the service use command —

$ pm2 stop <app name>

To delete the service us command —

$ pm2 delete <app name>

Please note that, this is a quick & simple way, but not the best way to schedule a service with PM2, kindly visit PM2 official site for more information.

You can start Node.js server as well by following the similar steps :)

happy coding…

--

--

Nihal Khan
Nihal Khan

Written by Nihal Khan

I’m predominantly a JavaScript developer with 11+ years of development experience in building software with Node, React Native, ReactJs , Angular and OOJS .

No responses yet