0

I am trying to manage my SvelteKit build with PM2 (Process Manager) — my problem is that I can't succesfully inject a .env-file using an ecosystem.config.cjs. My files currently look like this:

.env.production

PORT=3000

The only changing thing in both configs is at: env: { }

ecosystem.config.cjs (working fine - app runs on provided port)

module.exports = {
    apps: [
        {
            name: 'my_app',
            script: './build/index.js',
            watch: false,
            ignore_watch: ['database'],
            autorestart: true,
            // --------------------------------------------------
            // if passed directly PORT is being used as expected:
            // --------------------------------------------------
            env: {
                PORT: 3000
            }
        }
    ]
};

ecosystem.config.cjs (not working - injected PORT variable is being ignored)

module.exports = {
    apps: [
        {
            name: 'my_app',
            script: './build/index.js',
            watch: false,
            ignore_watch: ['database'],
            autorestart: true,
            // ----------------------------------------------------
            // when I try to inject a .env it's just being ignored:
            // ----------------------------------------------------
            env: {
                ENV_PATH: "./.env.production",
            }
        }
    ]
};

Any help is much appreciated and thanks for reading! Cheers, Boris

EDIT: Made question a bit more clear + added answer below

SMEETT
  • 166
  • 1
  • 9
  • You say (not working) can you explain? Do you have error messages? error logs? etc. – Alaindeseine Aug 31 '22 at 12:11
  • @Alaindeseine No, no error logs since there was no error (just the env-var being ingnored). But I figured it out, so thanks for reminding me to close this. Answer incoming. – SMEETT Sep 01 '22 at 14:01

1 Answers1

0

The problem wasn't the injection of .env.production, but the PORT environment variable. PORT must be provided directly and can't be part of .env.production (well, it can be but will be ignored). There's probably another way, but the following works:

ecosystem.config.cjs

module.exports = {
    apps: [
        {
            name: 'my_app',
            script: './build/index.js',
            watch: false,
            ignore_watch: ['database'],
            autorestart: true,
            // ----------------------------------------------------
            // when I try to inject a .env it's just being ignored:
            // ----------------------------------------------------
            env: {
                PORT: 3000,
                ENV_PATH: "./.env.production",
            }
        }
    ]
};

.env.production

# production
PUBLIC_test=value
SMEETT
  • 166
  • 1
  • 9