Dialect needs to be explicitly supplied as of v4.0.0
Check the dialect once.
const Sequelize = require('sequelize');
// Option 1: Passing parameters separately
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
I was facing this error, as it turns out, because of typescipt's transformation/compilation.
A little background: I am using sequelize in a typescript project. And the database config file was in a database.ts
file.
const config = {
development: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
test: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
production: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
};
export default config;
In .sequelizerc
file, I was pointing to the transpiled version of the database.ts
file i.e. the dist/config/database.js
file. As shown below:
const path = require('path');
module.exports = {
env: process.env.NODE_ENV || 'development',
config: path.resolve('dist', 'config', 'database.js'),
...
};
But after inspecting the transpiled version of the database.ts
file, i noticed that the config was exported as:
module.exports.default = config
But sequelize
is expecting the config to be at module.exports
.
So, I modified the database.ts
file by appending this one line to the end of the file and that resolved it for me.
...
module.exports = config;
Solution for me was based on what I had set for my NODE_ENV
variable.
echo $NODE_ENV
If you do not have anything set for that variable, try setting it with the following:
export NODE_ENV=development
If a value is present, make sure you have an entry in your config file for that value. For me, I like to use local
. So I had to update my config to this:
{
local: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
development: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'postgres'
},
production: {
username: 'root',
password: null,
database: 'database',
host: '127.0.0.1',
dialect: 'postgres'
}
}