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

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

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 6
  • Comments: 20

Commits related to this issue

Most upvoted comments

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.

try NODE_ENV=db sequelize db:migrate your config file object first level key should be NODE_ENV value. i spend lot‘s 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];
      }

@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

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 have a similar issue, any help. ERROR: Error reading “src/core/database/config.ts”. Error: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts” for /Users/…/src/core/database/config.ts

Same issue for me