Issue with Mailgun credentials not loading in my Node.js application
I’m facing a problem where my Mailgun credentials aren’t being recognized after I deploy my Node.js app to Heroku. I’ve set my environment variables correctly using dotenv at the beginning of my script, but I still encounter an error indicating that the API key is undefined.
2021-08-27T20:08:00.618949+00:00 app[web.1]: throw new Error('apiKey value must be defined!')
2021-08-27T20:08:00.618949+00:00 app[web.1]: ^
2021-08-27T20:08:00.618950+00:00 app[web.1]: Error: apiKey value must be defined!
2021-08-27T20:08:00.618950+00:00 app[web.1]: at new Mailgun (/app/node_modules/mailgun-js/lib/mailgun.js:16:13)
What might be the reason for this error? I thought dotenv would correctly load the necessary environment variables.
had this exact issue last week lol. heroku completely ignores your .env file - that’s why u’r getting undefined. u need to set those vars through heroku config instead of relying on dotenv. easiest way is heroku config:set MAILGUN_API_KEY=yourkey from terminal, or just use the web dashboard config vars section.
Your .env file isn’t getting uploaded to Heroku - that’s the problem. Heroku doesn’t read local .env files since they’re usually in .gitignore anyway. You’ve got to set the environment variables directly in Heroku instead. Go to your app’s Settings tab in the Heroku dashboard and add them under Config Vars, or use the CLI: heroku config:set MAILGUN_API_KEY=your_key_here. Once you set these, Heroku makes them available as environment variables without needing dotenv. I ran into this exact issue on my first Heroku deploy and wasted hours debugging before I figured out Heroku handles env vars differently.
Double-check your Heroku environment variables first. Run heroku config to see what’s actually set - I’ve wasted hours on typos and trailing spaces in API keys. Make sure the variable names match exactly between your code and Heroku (case matters). dotenv works locally, but Heroku handles env vars differently.
Indeed, dotenv doesn’t function on Heroku as it doesn’t include your local .env file during deployment. I experienced a similar issue while deploying an Express application with APIs. You will need to input your environment variables directly into Heroku. Navigate to your app dashboard, then go to Settings and reveal the Config Vars section. Here, you can enter your MAILGUN_API_KEY and DOMAIN. Your existing code will remain unchanged; process.env will access the variables from Heroku’s config rather than from your local file. This approach also enhances security since your credentials won’t be exposed in your repository.
Yeah, everyone covered the Heroku config vars, but here’s what’ll save you way more headaches.
I dealt with this mess constantly - environment variables everywhere, API keys getting mixed up between staging and production, manually updating configs every deployment.
What changed everything? I automated the entire email workflow instead of wrestling with Mailgun configs. Set up Latenode to handle all email logic and credentials in one place.
Now I just hit a webhook endpoint for emails from any app. No environment variable headaches, no API key errors, and I can switch providers without touching my code.
The workflow handles credential storage, templates, delivery tracking, retries. Takes 10 minutes to set up and works across all environments.