cli icon indicating copy to clipboard operation
cli copied to clipboard

Unable to run migration in sequelize due to config file

Open kinsomicrote opened this issue 6 years ago • 20 comments

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

kinsomicrote avatar Apr 17 '18 16:04 kinsomicrote

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

mschipperheyn avatar Apr 18 '18 23:04 mschipperheyn

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];
      }

MiYogurt avatar Apr 25 '18 07:04 MiYogurt

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

kevincolten avatar May 16 '18 05:05 kevincolten

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:

    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
};```

ktranel avatar Jun 01 '18 20:06 ktranel

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.

Aniket-Singla avatar Sep 22 '18 17:09 Aniket-Singla

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 avatar Nov 27 '18 12:11 btronquo

@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

kevincolten avatar Nov 27 '18 21:11 kevincolten

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

btronquo avatar Nov 30 '18 12:11 btronquo

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

pisangGoreng avatar Mar 11 '19 16:03 pisangGoreng

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

kevincolten avatar Mar 11 '19 17:03 kevincolten

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
  }
};

pisangGoreng avatar Mar 12 '19 03:03 pisangGoreng

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

rhythm2611 avatar Apr 17 '20 10:04 rhythm2611

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

Chrisu892 avatar Apr 18 '20 20:04 Chrisu892

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.

rhythm2611 avatar Apr 19 '20 11:04 rhythm2611

Does anybody found a good solution for this?

mohammad-hammal avatar Jun 10 '20 09:06 mohammad-hammal

Same issue for me

bhavin-nakrani avatar Sep 20 '21 17:09 bhavin-nakrani

Any update? or any solution for this?

ALTELMA avatar Mar 22 '22 19:03 ALTELMA

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

halilbaydar avatar May 29 '22 10:05 halilbaydar

@halilbaydar,

There are some workarounds you can find online if you want to use typescript but the CLI is expecting a JavaScript file. Try changing config.ts to config.js and you should no longer see this specific error.

colinmcdonnell avatar Sep 27 '22 18:09 colinmcdonnell

Buenas noches mismo problema Sequelize CLI [Node: 18.13.0, CLI: 6.6.1, ORM: 6.32.1]

Loaded configuration file "config\config.js".

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

alguien me puede ayudar no he podido hacer las migraciones

sequalizerc.js: module.exports = { 'config': './db/config.js', 'models-path': './db/models/', 'migrations-path': './db/migrations/', 'seeders-path': './db/seeders/', }

config/config.js:

require('dotenv').config();

const config = { env: process.env.NODE_ENV || 'dev', port: process.env.PORT || 3000, dbUser: process.env.DB_USER, dbPassword: process.env.DB_PASSWORD, dbHost: process.env.DB_HOST, dbName: process.env.DB_NAME, dbPort: process.env.DB_PORT, }

module.exports = { config };

sequalize.js:

const { Sequelize } = require('sequelize');

const { config } = require('./../config/config'); const setupModels = require('./../db/models');

const USER = encodeURIComponent(config.dbUser); const PASSWORD = encodeURIComponent(config.dbPassword); const URI = postgres://${USER}:${PASSWORD}@${config.dbHost}:${config.dbPort}/${config.dbName};

const sequelize = new Sequelize(URI, { dialect: 'postgres', logging: true, });

setupModels(sequelize);

module.exports = sequelize;

migrations:

'use strict';

const {UserSchema, USER_TABLE} = require('./../models/user.model');

/** @type {import('sequelize-cli').Migration} */ module.exports = { async up (queryInterface) { await queryInterface.createDatabase(USER_TABLE, UserSchema); },

async down (queryInterface) { await queryInterface.dro(USER_TABLE); } };

db/config.js:

const { config } = require('./../config/config');

const USER = encodeURIComponent(config.dbUser); const PASSWORD = encodeURIComponent(config.dbPassword); const URI = postgres://${USER}:${PASSWORD}@${config.dbHost}:${config.dbPort}/${config.dbName};

module.exports = { development: { url: URI, dialect: 'postgres', }, production: { url: URI, dialect: 'postgres', } }

.env:

PORT=3000 DB_USER='alru' DB_PASSWORD='admin123' DB_HOST='localhost' DB_NAME='my_store' DB_PORT='5432' DB_DIALECT='postgres'

estos son mis archivos alguien me puede decir como puedo solucionarlo muchas gracias

Alexis-Rua avatar Jun 30 '23 05:06 Alexis-Rua