Deploying Node.js app with Heroku config vars
Lorna Watson
Posted on March 6, 2021
I tried to deploy my Node.js project via Heroku and got the following error message:
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;
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);
});
};
Solution ✅
Add your new config vars in Heroku and then access them in your code like process.env.TEAM_CITY_BASE_URL
.
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);
});
};
🤩 Website loads perfectly with no errors 🤩
💖 💪 🙅 🚩
Lorna Watson
Posted on March 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.