I have a .env file that stores two different variables:
x=123
y=456
In the same folder, I have three other files, with a tree below:
/folder
/.env
/config.js
/app_setup.js
/match_algo.js
In my config.js file, I am reading the variables inside .env file and exporting them:
const dotenv = require('dotenv');
const cfg = {};
dotenv.config({path: '.env'});
cfg.port = process.env.PORT;
cfg.x = process.env.x;
cfg.y = process.env.y;
module.exports = cfg;
In the app.setup.js file, I am using the variable x
from .env:
var firebase = require("firebase");
const Config = require('./config');
var config = {
apiKey: Config.x
};
firebase.initializeApp(config);
module.exports = firebase;
In match_algo, I am using the variable y
from .env to do something else:
const Router = require('express').Router;
const Config2 = require('./config');
const router = new Router();
exports.return_match_uid = function return_match_uid() {
var variable2 = Config2.y;
.....
Apparently, the variable y
in match_algo is not read properly. I did not see any problem with my codes.
When you want to give a path to dotenv config, you need to give the proper path. Not just the .env
. See this
const dotenv = require('dotenv');
const cfg = {};
dotenv.config({path: './folder/.env'});
cfg.port = process.env.PORT;
cfg.x = process.env.x;
cfg.y = process.env.y;
module.exports = cfg;
Hope this helps