DEV Community

Lorna Watson
Lorna Watson

Posted on • Updated on

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 🀩

Top comments (0)