DEV Community

Lorna Watson
Lorna Watson

Posted on • Edited on

4 3

Deploying Node.js app with Heroku config vars

I tried to deploy my Node.js project via Heroku and got the following error message:

image

To see more details, I cd into the project’s directory and enter heroku logs --tail. The error is Cannot find module '../../config'.

config.js file 🧾

I have a config.js file at the project root (and is included in .gitignore so secrets not exposed!!):

var config = {};

config.baseUrl = "http://teamcity:8111/app/rest";
config.apiKey = "XXX";

module.exports = config;
Enter fullscreen mode Exit fullscreen mode

Function before

My code looked like this (striped out irrelevant bits):

const axios = require('axios'),
    config = require("../../config"); // ✨

exports.getAll = (req, res) => {
    axios({
        method: "get",
        url: `${config.teamCityBaseUrl}/builds`,
        headers: { 'Authorization': config.teamCityApiKey }
    }).then(response => {
        res.send(response.data);
    }).catch(error => {
        console.log(error);
    });
};
Enter fullscreen mode Exit fullscreen mode

Solution ✅

Add your new config vars in Heroku and then access them in your code like process.env.TEAM_CITY_BASE_URL.

image

Function after

const axios = require('axios');

exports.getAll = (req, res) => {
    axios({
        method: "get",
        url: `${process.env.TEAM_CITY_BASE_URL}/builds`,
        headers: { 'Authorization': process.env.TEAM_CITY_API_KEY}
    }).then(response => {
        res.send(response.data);
    }).catch(error => {
        console.log(error);
    });
};
Enter fullscreen mode Exit fullscreen mode

🤩 Website loads perfectly with no errors 🤩

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay