Search code examples
reactjsnginxbuildpm2production

MERN application running in 'development' through PM2


I have this line in the App.js of my React app:

console.log(process.env.NODE_ENV);

When running the app in development I see 'development' in the console as expected.

When I run npm run build then serve my app with serve -s build I then see 'production' in the console, which is great.

But when I run npm run build using nginx and try to launch my app with pm2 start npm -- start I still see 'development' in the console.

I have tried using serve in production which seems to work as a temporary fix but my session soon times out and the entire application gets disconnected.

I have also tried creating a new PM2 configuration file like this:

//ecosystem.config.js
module.exports = {
  apps: [
    {
      name: 'my-react-app',
      script: 'serve',
      args: ['-s', 'build'],
      instances: 1,
      autorestart: true,
      watch: false,
      max_memory_restart: '1G',
      env: {
        NODE_ENV: 'production',
      },
    },
  ],
};

and starting my app with the command pm2 start ecosystem.config.js

But I still see 'development' in the console.

I have also tried adding the --env production flag to the pm2 start command like this pm2 start --env production npm start but still the same result.

Any help or ideas would be hugely appreciated.


Solution

  • Solved the issue thanks to this answer https://stackoverflow.com/a/74126120/13146212

    Key was to use serve with pm2

    Hope this helps someone else out someday