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#Qn@^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...
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
},
...
};