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 🤩

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️