Skip to main content

How to create build of Your React App and server it for Forever using PM 2

Solution 1: Use Node.js's native import with pm2

You can directly use the native node command with pm2 to run the serve command. Here's how:

  1. Install serve as a local dependency if you haven't already:

    bash

    npm install serve
  2. Start the app using pm2 with node and serve:

    bash
    pm2 start "node --no-warnings --experimental-json-modules ./node_modules/.bin/serve" --name "react-app" -- -s build

Solution 2: Use a serve script in package.json

Another approach is to create a script in your package.json to run serve, and then use pm2 to start that script:

  1. Add a script to package.json: ( package.json of frontend folder )


    { "scripts": { "start:serve": "serve -s build" } }
  2. Use pm2 to run the script:



    pm2 start npm --name "react-app" -- run start:serve

Solution 3: Using a custom script with pm2

If you want to avoid npm scripts, you can create a custom Node.js script to run serve using ES Module syntax.

  1. Create a script file named serve-app.mjs:


    import serve from 'serve'; const server = serve('build', { port: 3000 });
  2. Start the script using pm2:


    pm2 start serve-app.mjs --name "react-app"

Explanation

  • ESM vs CommonJS: serve is an ES Module, and pm2 by default runs scripts as CommonJS. You need to ensure that pm2 is aware that it should handle the script using ES Module semantics, either by using Node.js directly or by setting up your pm2 process correctly.

  • Using Node with --no-warnings: The --no-warnings flag suppresses any experimental warnings that might be shown when using ES Modules with Node.js.



Comments

Popular posts from this blog

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  ...

How to deploy NodeJS app on server with Apache2 and acess it with Server IP addresss - Node JS deploy project Live.

     hello friens , This is Vasu Birla , in previous post we have seen the deployement of NodeJS app on AWS instance server instance but AWS server is expensive than other servers . SO today we will use simple Ubuntu server for making Live out Project using Apache2 . At the end you will be abe to access your NodeJS app using server IP address from anywhere  .. 1.  Login to your server using SSH terminal . (in AWS part i already explained how to do this )     -> Open your putty in hostname put your server IP -      login with ssh username ->root and password     (if you dont have root username and pass ask your      provider or reset it from cpanel or hosting panel ) 2. after login - on terminal you can Put your project anywhere.     There are two ways to put your project folder on server location     (i) - > using Github - (very popular and easy to track your everyday code changes t...

GitHub Repo Collaboration Work on single project

 =============================================== To collaborate effectively with your friend on the same project, you should use Git branches to manage different lines of development. Here's a step-by-step procedure you can follow to streamline collaboration: 1. Create Separate Branches for Each Developer Create a New Branch for Your Friend: On your local repository, create a new branch for your friend. For example if your friend name is kilvish , if you want to create a branch named kilvish , you would run: make sure you would be on main branch already   command->  git checkout -b kilvish git push origin kilvish 2. Set Up Your Friend’s Environment( On your Friend's System )  at kilvish side  run ->  Clone the Repository (if not already done): If your friend hasn’t cloned the repository yet, they should do so: command ->  git clone https://github.com/Vasu-Birla/your-repo.git   // your your main clone line  Fetch All Bran...