41

I'm looking to use an environment variable inside of the config.json file of my project using sequelize. I'm using dotenv to set environment variables locally. My config.json file looks like this

{
  "development": {
    "username": process.env.DB_USER,
    "password": process.env.DB_PASS,
    "database": process.env.DB_DATABASE,
    "host": process.env.DB_HOST,
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "use_env_variable": "JAWSDB_URL",
    "dialect": "mysql"
  }
}

The issue I'm having is that I can't use variables inside the config.json file. It looks like for production I can use the "use_env_varable" key and use the env variable for my connection string. So I guess I either need a way to figure out the combined connection string for my local mysql db or a way to use variables inside the config.json. Any solutions?

6 Answers 6

92

you should change config.json file to a config.js module and make sure to require the dotenv at the very top.

require('dotenv').config(); // this is important!
module.exports = {
"development": {
    "username": process.env.DB_USERNAME,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_DATABASE,
    "host": process.env.DB_HOST,
    "dialect": "mysql"
},
"test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
},
"production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
}
};

NOTE: update your .sequelizerc file to match the new config file.

"config": path.resolve('./config', 'config.js'),
3
  • Just a hunch, follow the link above to see how to update your .sequelizerc file from their documentation .sequelizerc docs Jul 9, 2021 at 4:24
  • 1
    After struggling for an hour, then try googling and find your answer. My problem is solved. Thank you
    – Wisnu
    Jan 5, 2022 at 0:20
  • 1
    How can i make it work with NextJS where I'm not able to use dotenv? Sep 2, 2022 at 13:47
2

I worked on this for quite a bit. I do not know why Sequelize does not use production when it is literally in the environment if you run heroku run bash. I was able to get it working by modifying the Sequelize object depending on the JAWSDB_URL, not the NODE_ENV.

require("dotenv").config();
const express = require("express")
const app = express();
let seq;

//express app configuration

if (process.env.JAWSDB_URL) {
    console.log("There is a JAWS DB URL")
    seq = new Sequelize(process.env.JAWSDB_URL)
}
else {
    seq = require("./models").sequelize
}
seq.sync().then(() => {
  app.listen(PORT, () => console.log('server started on port ' + PORT));
})
1

Create a .sequelizerc file in the root directory.

var path = require('path');
module.exports = { 
'config': path.resolve('server/config', 'config.js'),
'models-path': path.resolve('server/models'),
'seeders-path': path.resolve('server/seeders'),
'migrations-path': path.resolve('server/migrations')}

Then create config.js in config folder. Use following code in config.js

const dotenv = require("dotenv");
dotenv.config({ path: "config.env" });

module.exports = {
  development: {
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DBNAME,
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
    encrypt: process.env.DB_ENCRYPT,
    pool: {
      max: parseInt(process.env.DB_POOL_MAX),
      min: parseInt(process.env.DB_POOL_MIN),
      acquire: parseInt(process.env.DB_POOL_ACQUIRE),
      idle: parseInt(process.env.DB_POOL_IDLE),
    },
  },

After that run migration and seeders.

0

Assuming you're using Passport, Sequelize, MySql:

On the index.js file setup through sequelize, look for this line:

var sequelize = new Sequelize(config.database, config.username, config.password, config);

Try changing it to:

var sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, config);

Your .env should have:

DB_USERNAME:root (or whatever your username is)
DB_PASSWORD:NYB (whatever your password is)
DB_DATABASE:whatever_your_dbNameis_db

Last, depending on how your passport strategy is setup, look for something that says:

user.sequelize.sync().then(function(){
}...

You are going to need to place the: database:process.env.DB_DATABASE

user.sequelize.sync().then(function(){
     database:"process.env.dbn"
}...

This means that you should remove that key:value from the config.json.

It should be good to go and you will not have to convert anything. Sequelize will take care of all of that.

1
  • One thing I forgot to add is you may have to drop(reset) your database/table in order to for this work.
    – Rod_R
    May 11, 2018 at 0:54
0

Here is what my config.js file looks like:

const fs = require('fs');
require('dotenv').config();

module.exports = {
  "development": {
    "username": process.env.DB_USERNAME,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_DATABASE,
    "host": process.env.DB_HOST,
    "dialect": process.env.DB_CONNECTION
  },
  "test": {
    "username": process.env.DB_USERNAME,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_DATABASE,
    "host": process.env.DB_HOST,
    "dialect": process.env.DB_CONNECTION
  },
  "production": {
    "username": process.env.DB_USERNAME,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_DATABASE,
    "host": process.env.DB_HOST,
    "dialect": process.env.DB_CONNECTION
  }
};
-1

you can use the use_env_variable for development too. you already seem to be using dotenv, so im assuming you have a .env file already setup. just add this line to it:

LOCALDB=mysql://[user]:[pass]@[sqldomain]/[db name]

replace the stuff in [] as necessary, and in your config file set "use_env_variable" to LOCALDB, just like how you have JAWSDB for production.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.