I have a Node.js API that stores environment variables in a file called config.env
.
When I am not using pm2, I launch the server with the following command: NODE_ENV=prod nodemon --env-file config.env server.js
. It works properly and I can access the environment variables.
I would like to have access to the environment variables when I start the server with pm2. I tried this command, but it doesn't work: pm2 start server.js -- --env-file config.env
.
Try to use cross-env Cross-env npmjs.com
It would help if you had cross-env NODE_ENV=production something like this in your script and then run your pm2 using
pm2 start npm --name "{app_name}" -- run {script_name}
Then configure your env file in nodejs app conditionally like if process.env.NODE_ENV equals to production then read the environments from the corresponding file