PM2 not passing environment variables (AWS EC2)

System Information
  • Strapi Version: 4.8.2
  • Operating System: Ubuntu 20.04
  • Database: PostgreSQL 14.7
  • Node Version: 18.14.2
  • NPM Version: 9.5.0
  • Yarn Version: (not installed)

Hi all,

I’m stuck deploying Strapi to an AWS EC2 instance in a VPC. I’m following the Strapi doc for installing Strapi v4 on AWS:

I created an .env file manually on this server (not committed to git) which specifies all the environment variables (database connections, etc.). The application starts successfully if I SSH into the EC2 instance and run npm run start.

However, after installing PM2 and modifying the ecosystem.config.js file to include the same environment variables in the env property (per the PM2 support docs), the application doesn’t start in the background.

Running pm2 logs 0 outputs the error:

Error: Could not load js config file /var/www/html/config/database.js: env is not defined

This file config/database.js has all the environment variable references such as: host: env('DATABASE_HOST'), etc. (which, again, work when running npm run start while connected via SSH).

It seems like PM2 is just not passing the correct environment variables to the node process…how can I fix this?

Thanks so much.