Cli: Unable to run migration in sequelize due to config file

Created on 17 Apr 2018  路  15Comments  路  Source: sequelize/cli

What you are doing?

I am having an issue with my config file I have it set up to make use of environment variables It looks like this

module.exports = {
  db: {
    username: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME,
    options: {
      host: process.env.HOST || "127.0.0.1",
      dialect: process.env.DIALECT || "postgres"
      }
    }
  }

when I want to run migrations and I am faced with two errors;

1

Loaded configuration file "config/config.js".

ERROR: Dialect needs to be explicitly supplied as of v4.0.0

2

ERROR: password authentication failed for user "jioke"
My models/index.js file looks like this;

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(__filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/../config/config.js');
var db        = {};

const sequelize = new Sequelize(
  config.db.database,
  config.db.user,
  config.db.password,
  config.db.options
)

....

After searching on Google, I created a .sequelizerc file.

const path = require('path');

module.exports = {
  'config': path.resolve('config', 'config.js')
}

My .env file looks like this;

DB_USER='test'
DB_PASS='test'
DB_NAME='test'
DIALECT='postgres'

None of the solutions I found on Google seems to work

The only that one worked; was converting the config.js file to config.json. But I want to make use of environment variables so I don't commit what I have in my .env file. What is the way around/out of this? Thanks.

I posted on the Slack group, but no response still, so I decided to post here.

__Dialect:__ postgres
__Database version:__ XXX
__Sequelize CLI version:__ XXX
__Sequelize version:__ 4.37.6

Most helpful comment

Hello, I had the same problem. Using the 'dotenv' package solved this problem for dynamic configuration of Sequelize. Do require('dotenv').config() at the top of your configuration file for Sequelize. Hope this helps.

All 15 comments

I'm also having problems, but your config structure looks wrong. The key db should be development, production or test and dialect and host should be in root. I think. Can't get it to work myself either

try NODE_ENV=db sequelize db:migrate your config file object first level key should be NODE_ENV value. i spend lot鈥榮 time find them.

in source code

https://github.com/sequelize/cli/blob/1c8983c69b921b2c43ecfc1062449bed143c22e8/src/core/yargs.js#L22

  return yargs
    .option('env', {
      describe: 'The environment to run the command in',
      default: 'development',
      type: 'string'
    })

https://github.com/sequelize/cli/blob/1c8983c69b921b2c43ecfc1062449bed143c22e8/src/helpers/generic-helper.js#L10

getEnvironment: () => {
    return args.env || process.env.NODE_ENV || 'development';
  },

https://github.com/sequelize/cli/blob/1c8983c69b921b2c43ecfc1062449bed143c22e8/src/helpers/config-helper.js#L128

      if (api.rawConfig[env]) {
        helpers.view.log('Using environment "' + env + '".');

        api.rawConfig = api.rawConfig[env];
      }

You can map your sequelize command to inject your .env file the package.json scripts
"sequelize": "env $(cat .env | grep -v ^# | xargs) sequelize"
Now you can run
yarn sequelize db:migrate

I'm writing this out for anyone else who happens to come across it. This took me a while to figure out. So, the sequelize cli is expecting you js file to look something like this:

```module.exports = {
dev:{
username:user,
password:somepassword,
etc...
},
prod:{}
}

however, you've put your variables just a level lower and now it is confused on where to look. This is basically a scope/variable definition issue and IMHO something sequelize needs to address because their ENV config method is definitely not the most production friendly in the world. 

what I ended up needing to do was this:

const dev = {
// use_env_variable:false,
app: {
port: parseInt(process.env.PORT) || 3306
},
db: {
username: envUsername,
password: envPassword,
database: envDatabase,
host: envHost,
dialect: envDialect
},
//uncomment the below keys when seeding the database
sessionKey:evnSessionKey,
username: envUsername,
password: envPassword,
database: envDatabase,
host: envHost,
dialect: envDialect
};```

Hello, I had the same problem. Using the 'dotenv' package solved this problem for dynamic configuration of Sequelize. Do require('dotenv').config() at the top of your configuration file for Sequelize. Hope this helps.

Hello,
Same problem for me

Can't figure out why this config doesn't work for db:migrate!

I re-wrote my code like this, but always the same problem..

./config/config.js

require('dotenv').config()

module.exports = {
  port: process.env.PORT || 8081,
  db: {
    database: process.env.DB_NAME || 'raptorvue',
    user: process.env.DB_USER || 'raptorvue',
    password: process.env.DB_PASS || 'raptorvue',
    options: {
      dialect: process.env.DIALECT || 'mysql',
      host: process.env.HOST || 'localhost'
    }
  },
  username: 'raptorvue',
  password: 'raptorvue',
  database: 'raptorvue',
  host: 'localhost',
  dialect: 'mysql'
}

If someone can help me, it will be very very cool ;D

@btronquo I've since installed dotenv-cli and then made a script in my package.json: "sequelize": "dotenv sequelize" so I now run yarn sequelize db:migrate

wow, thanks @kevincolten , it all works now 馃憤

So for people in my case:

npm install --save dotenv

And ..

.env

DB_HOST=127.0.0.1
DB_USER=user
DB_PASS=pass
DB_NAME=dbname
DB_DIALECT=mysql

config/config/.js

require('dotenv').config()
module.exports = {
  'development': {
    'username': process.env.DB_USER,
    'password': process.env.DB_PASS,
    'database': process.env.DB_NAME,
    'host': process.env.DB_HOST,
    'dialect': process.env.DB_DIALECT
  },
  'test': {
    'username': process.env.DB_USER,
    'password': process.env.DB_PASS,
    'database': process.env.DB_NAME,
    'host': process.env.DB_HOST,
    'dialect': process.env.DB_DIALECT
  },
  'production': {
    'use_env_variable': 'MYSQL_URL',
    'dialect': 'mysql'
  }
}

models/index.js

'use strict'

var db = {}
var fs = require('fs')
var path = require('path')
var Sequelize = require('sequelize')
var basename = path.basename(__filename)
var env = process.env.NODE_ENV || 'development'
var config = require('./../config/config.js')[env]

var sequelize
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config)
}
... and the rest of the code

package.json

  "scripts": {
    "sequelize": "dotenv sequelize",
  },

To run migrate/seed/other, use for example sequelize db:migrate

Hi @btronquo , I try with your solution, but still not work

@pisangGoreng I think that last command should be yarn sequelize db:migrate or npm sequelize db:migrate if that helps

Hi @kevincolten , I'm already trying yarn sequelize db:migrate or npm sequelize db:migrate command, but still not works. Finally my sequelize db:migrate can run well, after changing config property from development to dev. I don't know why, but it works

require("dotenv").config();

const {
  TRUCKWAY_AUTH_DB_NAME,
  TRUCKWAY_AUTH_DB_USER,
  TRUCKWAY_AUTH_DB_PASSWORD,
  TRUCKWAY_AUTH_DB_DIALECT,
  TRUCKWAY_AUTH_DB_HOST_MASTER
} = process.env;

module.exports = {
  dev: {
    username: TRUCKWAY_AUTH_DB_USER,
    password: TRUCKWAY_AUTH_DB_PASSWORD,
    database: TRUCKWAY_AUTH_DB_NAME,
    host: TRUCKWAY_AUTH_DB_HOST_MASTER,
    dialect: TRUCKWAY_AUTH_DB_DIALECT
  },
  production: {
    username: TRUCKWAY_AUTH_DB_USER,
    password: TRUCKWAY_AUTH_DB_PASSWORD,
    database: TRUCKWAY_AUTH_DB_NAME,
    host: TRUCKWAY_AUTH_DB_HOST_MASTER,
    dialect: TRUCKWAY_AUTH_DB_DIALECT
  }
};

I am facing problem while migrating in production because i have used dotenv as dev dependency.

-------.sequelizerc file ------

`'use strict';

require('dotenv').config(); // I cannot import in production . It works fine in development

const path = require('path')

module.exports = {
"config": path.resolve('./config', 'config.js')
}`

-----config.js file-----

`module.exports = {

"development": {
  "username": process.env.DB_USER,
  "password": process.env.DB_PASS,
  "database": process.env.DB_DATABASE,
  "port": process.env.DB_PORT,
  "host": process.env.DB_HOST,
  "dialect": process.env.DB_DIALECT,
  "operatorsAliases": false
},
"test": {
  "username": process.env.DB_USER,
  "password": process.env.DB_PASS,
  "database": process.env.DB_DATABASE,
  "port": process.env.DB_PORT,
  "host": process.env.DB_HOST,
  "dialect": process.env.DB_DIALECT,
  "operatorsAliases": false
},
"production": {
  "username": process.env.DB_USER,
  "password": process.env.DB_PASS,
  "database": process.env.DB_DATABASE,
  "port": process.env.DB_PORT,
  "host": process.env.DB_HOST,
  "dialect": process.env.DB_DIALECT,
  "operatorsAliases": false
}

}
`

only problem exists while using sequelize-cli commands in production because env values are undefined

Adding .sequelizerc file with the following code in the root directory solved my problem.

const 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')
}

Hello Chrisu892,
Yes, I have used .sequelizerc file in root directory. can you please explain me why env constant r undefined only in case of migration ( for sequelize-cli command ) but any database operation works well.

Does anybody found a good solution for this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

papb picture papb  路  3Comments

gotrevor picture gotrevor  路  5Comments

OsoianMarcel picture OsoianMarcel  路  4Comments

eharoldreyes picture eharoldreyes  路  3Comments

arndeash picture arndeash  路  3Comments