Search code examples
node.jsherokuerror-handlingmailgun

Mailgun not seeing credentials in Node.js app


I'm having an issue where Mailgun isn't seeing the apiKey when I upload it to Heroku. I have my environment variables set and my dotenv at the top of my code and yet it still returns the error. Is there something I'm missing?

  • In my app.js
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
// MAILGUN INFO
let api_key = process.env.MAILGUN_API_KEY
let domain = process.env.DOMAIN 
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

//DATA PARSING AND VIEW ENGINE
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.set("view engine", 'ejs');
  • My dotenv creditials
MAILGUN_API_KEY=******************************
DOMAIN=*********.com

  • Error returned in the logs
2021-08-27T20:07:53.000000+00:00 app[api]: Build succeeded
2021-08-27T20:07:58.685692+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-08-27T20:08:00.618937+00:00 app[web.1]: /app/node_modules/mailgun-js/lib/mailgun.js:16
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.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)
2021-08-27T20:08:00.618950+00:00 app[web.1]: at create (/app/node_modules/mailgun-js/lib/mailgun.js:239:10) 
2021-08-27T20:08:00.618950+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:11:36)
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)    
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-08-27T20:08:00.618953+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-08-27T20:08:00.666412+00:00 heroku[web.1]: Process exited with status 1
2021-08-27T20:08:00.719053+00:00 heroku[web.1]: State changed from starting to crashed

Solution

  • Your .env file should only be used for local configuration.

    "The .env file lets you capture all the config vars that you need in order to run your app locally." -- https://devcenter.heroku.com/articles/heroku-local

    Use the heroku_config commands or Heroku's GUI to set up environment variables for your app when deployed onto Heroku's servers. More details can be found on Heroku's website.