juls
juls

Reputation: 85

[PM2][WARN] Environment [production] is not defined in process file - while using pm2 static serve by ecosystem.config.js

Using PM2 as server management, I used pm2 serve in ecosystem.config.js for serving my react app

Here is my ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "my-app",
      script: "serve",
      instances: "1",
      watch: false,
      exec_mode: "cluster",
      restart_delay: "5000",
      listen_timeout: 50000,
      kill_timeout: 5000,
      log_date_format: "YYYY-MM-DD HH:mm Z",
  env:{
       NODE_ENV: "production",
       PM2_SERVE_PATH: 'build',
       PM2_SERVE_PORT: 3000,
    },
    },
  ],
};

The reason I wrote script as serve is for using pm2 serve as static server for index.html which was build. Here is the document that I referenced.

But PM2 cannot find my .env.production file saying Environment [production] is not defined in process file.

Is there other ways to set env process file for production? Also I find out running the app in port 3000 only works in PM2_SERVE_PORT: 3000 when I use script: serve.

Another way doesn't make my app runs in port 3000.

So I need a way for using both values(NODE_ENV, PM2_SERVE_PORT, etc) in ecosystem.config.js settings.

Upvotes: 0

Views: 1606

Answers (1)

Dale Ryan
Dale Ryan

Reputation: 843

The following error happens if there is no env_production defined in your app in ecosystem.config.js but you are running pm2 like pm2 start ecosystem.config.js --env production:

Environment [production] is not defined in process file

In my case I have the following for my sveltekit app:

module.exports = {
  apps : [
    {
      name: "service:name",
      script: "./build/index.js",
      error_file: "./storage/log/error.log",
      out_file: "./storage/log/app.log",
      env: {
        NODE_ENV: 'development',
        PORT: 3000,
        ORIGIN: "http://localhost:3000",
        BODY_SIZE_LIMIT: 0
      },
      env_production: {
        NODE_ENV: 'production',
        PORT: 3000,
        ORIGIN: "https://my-sample-domain.com",
        BODY_SIZE_LIMIT: 0
      },
    }
  ]
}

Running pm2 start ecosystem.config.js will default to using the env property when setting variables. In this case it uses the following:

env: {
  NODE_ENV: 'development',
  PORT: 3000,
  ORIGIN: "http://localhost:3000",
  BODY_SIZE_LIMIT: 0
},

Meanwhile, running pm2 start ecosystem.config.js --env production will make pm2 look for env_production key in the app config. No need to create a separate file for production config, just add env_production property and define the production settings there then run pm2 start ecosystem.config.js --env production.

Documentation about Switching environments in PM2.

Upvotes: 1

Related Questions