my .env variables wont return for database login in node.js

113 Views Asked by At

Having a problem with .env variables in node.js, am using dotenv to handle the variables.

straight forward

require('dotenv').load()

and then checked console log to see if I can access then variables like so

console.log("database Name: "+process.env.DB_NAME);
console.log("database Username: "+process.env.DB_USER);

etc and all return fine.

the part of the code that calls the db is the following-

var Sequelize = require('sequelize');

module.exports = new Sequelize('dbname, 'username',  'password', {
host: 'hostname',
dialect: 'mssql',
dialectOptions: {
    instanceName: 'namehere'
}

});

I replace the dbname with process.env.DB_NAME and all is fine, nothing falls over but the moment I try and use DB_USER in there then it fails to connect and shows user as ' ' in the log.

 module.exports = new Sequelize(process.env.DB_NAME, process.env.USER_DB,  'password', { etc

the .env file itself is very straight forward and nothing out of the ordinary. structured as

NAME="value" 
NAME2="value2"

etc. I though maybe once a value has been console logged it wont be available and turned off the console logs, but no luck. I also tried changing the name of the value encase that was reserved. please advise.

1

There are 1 best solutions below

0
Ray_Hack On

nevermind I was being silly. I presumed it only needed to be required once but not the case.