I have a backend that was working without errors, so I’m trying to send it to Heroku Server, as it is my first trying with Heroku, I’ve chosen a free database (JawsDB MySQL), but trying to change my local database MySQL to the one on Heroku my .env file is not working anymore, I’ve already tried to change several things there, but I have always an error related to my .env file
.env:6 DB_HOST=qao3ibsa7hhgecbv.cbetxkdyhwsb.us-east-1.rds.amazonaws.com,
Installing my database on MySQL workbench I get no errors, but I can’t find a way to set it on my .env file
Here is my .env file:
require('dotenv').config(); module.expots = { authSecret: 'myauthsecret', db: { DB_HOST=dbhost.us-east-1.rds.amazonaws.com, DB_USER=dbuser, DB_PASSWORD=dbpassword DB_PORT=3306, DB_DATABASE=dbname } }
My auth secret is used by the passport.js
And here is my knexfile.js where I set my database credentials:
const { db } = require('./.env'); module.exports = { client: 'mysql2', connection: {db, mysqlhost: process.env.DB_HOST, username: process.env.DB_USER, password: process.env.DB_PASSWORD, port: process.env.DB_PORT, database: process.env.DB_DATABASE }, pool: { min: 2, max: 10 }, migrations: { tableName: 'knex_migrations' } };
I have no idea what can be so wrong with that, On my local machine I used to set my .env like this:
module.exports = { authSecret: 'sqt;n/eP-kX-(J#I#8WahB|uhx/i)^$aZ[ZD!PADRZ#<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7b689a7">[email protected]</a>^s;Sl]~wI?3ADxF', db: { host: '127.0.0.1', port: '3306', user: 'root', password : 'aPassword', database : 'database' } }
And it was working, now I have no idea what I should do to make it working with my database on Heroku…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The error you are getting is because your are loading the .env
as JS code and the db
object is not valid JS. It should be like this to be valid JS:
module.expots = {
authSecret: 'myauthsecret',
db: {
DB_HOST: "dbhost.us-east-1.rds.amazonaws.com",
DB_USER: "dbuser",
DB_PASSWORD: "dbpassword",
DB_PORT: 3306,
DB_DATABASE: "dbname",
}
}
That said, there is a misunderstanding on what a .env
file should have and how to use it. The correct .env
content shoul be just:
# .env # Replace with the correct values DB_HOST=dbhost.us-east-1.rds.amazonaws.com DB_USER=dbuser DB_PASSWORD=dbpassword DB_PORT=3306 DB_DATABASE=dbname
Then in your knexfile.js
you do:
// This loads the .env variables into the process.env
require('dotenv').config();
module.exports = {
client: 'mysql2',
connection: {
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
database: process.env.DB_DATABASE
},
...
};
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0