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);
});
};
Top comments (0)