toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.73k stars 65 forks source link

It seems env-cmd not working with pm2 #315

Open ujjavaljani opened 3 years ago

ujjavaljani commented 3 years ago

Hello, I am using env-cmd in my typescript application. for production I am creating node js build using tsc and after build when I tried to pass custom env file then it throws error like .env file could not found.

image

I am trying to run command npm run pm2:start:qa.

Thanks in advance.

erassynathingo commented 2 years ago

Perhaps this could help. Create an pm2.config.js file, define your configs in there and access the ENV variables as show below.

module.exports = {
    apps: [
        {
            name: "app-namne",
            script: "./build/index.js",
            watch: true,
            ignore_watch: [
                ".git",
                ".md",
            ],
            time: true,
            env: {
                PORT: process.env.PORT,
            },
        },
    ],
};

qa start script should look something like this:

npm run build && env-cmd -f ./environments/.env pm2 start pm2.config.js",