node dotenv won’t work with pm2

dotenv will read .env file located in the current directory.

When you call pm2 start myapp/app.js it won’t search for myapp/.env.

.env // It will try to load this, which doesn't exist
myapp/
   app.js

So you have two solutions

use path option:

const path = require('path'); 
require('dotenv').config({ path: path.join(__dirname, '.env') });

Or call your script from inside myapp/

pm2 start app.js

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)