Open MonsieurJoyeux opened 7 months ago
If you run in fork mode, this should work correctly.
In cluster mode, the cwd option is not being used, which there is a related issue for https://github.com/Unitech/pm2/issues/5722
node_args: '-r dotenv/config'
works for me under pm2 5.3.1 cluster mode.
But it needs a clean pm2 daemon.
If I stop pm2 stop <app>
and restart pm2 start <app>
, there is an error says dotenv/config not found,
I have to run pm2 kill
, then run pm2 start <app>
in a new pm2 daemon.
Im trying to setup in production a nodeJs app in cluster mode with a .env file provided. I tried doing so using 2 approaches :
1 ) Using dotenv
I can get it working only if I change my application code adding "require('dotenv').config()". I don't want to delegate this responsability to the code. So I tried using this ecosystem file :
and when I execute "pm2 deploy ecosystem.config.js develop", my apps crash instantly and silently (pm2 logs doesn't log anything which is weird in a first place) I tried to play around with apps[0].env.DOTENV_CONFIG_PATH and apps[0].node_args, and never achieved to have a working clustered env for my app. It either crash or doesn't load env variables.
2 ) Using built-in NodeJS support for .env files
With this config :
In a first place it does work. Then I tried to add a second app clustered as well and I noticed something weird aswell : The first app that is launched will successfully load env variables, but all other apps launched after the first wont load them. I had "applicationA" and "applicationB", when I first launch "applicationB" and then "applicationA", I will have 2 nodes working for applicationB and 2 in errors for application A. If I launch application A first, its the other way around.
Im using Node 20.11.0, pm2 5.3.1 on Debian 12 Bookworm
Here is my pm2 reports
(The app "api-dev" is actually no working since it didn't load the env variables)
Did someone already achieved to launch in cluster mode, multiple nodeJS app with a .env file ? (Thats my first issue I submit, sorry if it isn't clear)