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
- Merge pull request #641 from exercism/download-uuid Fix problem with downloading another person's solution using the --uuid flag — committed to codetriage-readme-bot/cli by deleted user 6 years ago
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
https://github.com/sequelize/cli/blob/1c8983c69b921b2c43ecfc1062449bed143c22e8/src/helpers/generic-helper.js#L10
https://github.com/sequelize/cli/blob/1c8983c69b921b2c43ecfc1062449bed143c22e8/src/helpers/config-helper.js#L128
@btronquo I’ve since installed dotenv-cli and then made a script in my package.json:
"sequelize": "dotenv sequelize"
so I now runyarn sequelize db:migrate
wow, thanks @kevincolten , it all works now 👍
So for people in my case:
npm install --save dotenv
And …
.env
config/config/.js
models/index.js
package.json
You can map your
sequelize
command to inject your.env
file thepackage.json
scripts"sequelize": "env $(cat .env | grep -v ^# | xargs) sequelize"
Now you can runyarn 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