I have been working on a NodeJS project which uses PostgreSQL database. I am trying to implement migration to the database. Also, using Sequelize. After setting up the migration folder and config, it throws error while running db:migrate
The error is: "Dialect needs to be explicitly supplied as of v4.0.0"
121 Answers
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' }
} 4 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 = configBut 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; 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 got same error and I saw this mistake in the code.
title: { type: Sequelize, allowNull: false, },Changed my code with this and problem is solved:
title: { type: Sequelize.STRING, allowNull: false, }, Check your config file (env names)
{ development: { username: 'root', password: null, database: 'database_development', host: '127.0.0.1', dialect: 'mysql' }, test: { username: 'root', password: null, database: 'database_test', host: '127.0.0.1', dialect: 'mysql' }, production: { username: 'root', password: null, database: 'database_production', host: '127.0.0.1', dialect: 'mysql' }
} I think you have missed .env file in your project
My issue was that I wasn't pointing to the config file properly. It said "successfully loaded" but I wasn't pointing to the right file.
It should say "Loaded configuration file "[some path]/config.js"."
In my case issue was the way I was exporting config from config.tsexport const = {} did not work
but module.exports worked:
module.exports = { development: { dialect: process.env.DB_DIALECT, username: process.env.DB_USER, password: process.env.DB_PASS, database: process.env.DB_NAME_DEVELOPMENT, host: process.env.DB_HOST, port: process.env.DB_PORT, }, test: { dialect: process.env.DB_DIALECT, username: process.env.DB_USER, password: process.env.DB_PASS, database: process.env.DB_NAME_DEVELOPMENT, host: process.env.DB_HOST, port: process.env.DB_PORT, }, production: { dialect: process.env.DB_DIALECT, username: process.env.DB_USER, password: process.env.DB_PASS, database: process.env.DB_NAME_DEVELOPMENT, host: process.env.DB_HOST, port: process.env.DB_PORT, },
}; After pulling my hair out for a couple hours, I realized I was doing cd src; node app.js when really I was supposed to do node src/app.js... Gooooooodness
I had a config.js file from a previous project with a non-standard value for my environment.
It was called current.
I changed it to development and the error went away.
const main = require('./main');
module.exports = { development: { // this was set to `current` in my case, and it was causing the error username: main.db.user, password: main.db.password, database: main.db.name, host: main.db.host, port: main.db.port || 3306, dialect: 'mysql' }
}; 1 After reading through all the answers and possible solutions to problems people had and nothing worked for me here is another additional solution for an environment that uses:
- Typescript (Having the sequelize config in .ts)
- Sequelize
Problem: Not being able to run "sequelize db:migrate" (getting the error from this thread) due to sequelize not getting access to the .env file and cannot properly read the config
Example config
config: any = { "development": { "username": dbUser, "password": dbPassword, ... module.exports = configdbUser is undefined for Sequelize
- Create a ".sequelizerc" file in the root of your project and give it the path to the already built .js config file:
const path = require('path'); module.exports = { 'config': path.resolve('./dist', 'src/config/config.js') };- In your config file add
module.exports = config as sequelize does not like export default config (as mentioned above). I have both types of exports.
Install the dotenv-cli (I did it as a dev dependency, but globally should work fine as well)
Run the migration command while suppling the .env file with:
npx dotenv -e /path/to/.env sequelize db:migrate
Also had to make sure my NODE_ENV is set to development:
export NODE_ENV=development
0did you forget to add the dialect to your config? see:
if you have not setup any .env variables before running your npm server
You are likely to get that error. so each time you restart app for changes. you will have to export again
export DATABASE_URL=<your-db-url> This error can also be caused if there is an error in your model schema.
I had:
middle_name: { type: Sequelize.Sequelize, allowNull: false,
}Which should had been:
middle_name: { type: Sequelize.STRING, allowNull: false,
} In my case, I declared config.js like as:
module.exports = { production: { database: process.env.DB_PROD_DATABASE, username: process.env.DB_PROD_USERNAME, password: process.env.DB_PROD_PASSWORD, options: { host: process.env.DB_PROD_HOST, port: process.env.DB_PROD_PORT, dialect: 'postgres', define: { paranoid: true, timestamp: true, freezeTableName: true, underscored: false } } }, development: { database: process.env.DB_DEV_DATABASE || 'database_name', username: process.env.DB_DEV_USERNAME || 'user_name', password: process.env.DB_DEV_PASSWORD || 'pass', host: process.env.DB_DEV_HOST || 'localhost', port: process.env.DB_DEV_PORT || 5432, dialect: 'postgres', define: { paranoid: true, timestamp: true, freezeTableName: true, underscored: false } }
}But it should be like as:
module.exports = { production: { database: process.env.DB_PROD_DATABASE, username: process.env.DB_PROD_USERNAME, password: process.env.DB_PROD_PASSWORD, options: { host: process.env.DB_PROD_HOST, port: process.env.DB_PROD_PORT, dialect: 'postgres', define: { paranoid: true, timestamp: true, freezeTableName: true, underscored: false } } }, development: { database: 'database_name', username: 'user_name', password: 'pass', host: 'localhost', port: 5432, dialect: 'postgres', define: { paranoid: true, timestamp: true, freezeTableName: true, underscored: false } }
}without process.env.DB_DEV_DATABASE || 'database_name' eg.
If you are facing this error then,you have to add additional argument after password_for_rootUser in form of object and need to specify these property according to your RDBMS
var Sequilize=require('sequelize');
var connection =new Sequilize('db_name','root_user_name','password_for_rootUser',{ host:'localhost', dialect:'mysql'|'mariadb'|'sqlite'|'postgress'|'mssql', pool:{ max:5, min:0, idle:10000 },
//for sqlite only
storage:path/to/database.sqlite
});
var Article=connection.define('tableName',{ title:Sequilize.STRING, // your cloumn name with data type body:Sequilize.TEXT // your cloumn name with data type
});
connection.sync();hope this solves your problem
You can fix this by running this command
export NODE_ENV=development; npx sequelize db:migratecheers!
1In my case, I forgot to uncomment the DB_CONNECTION value from .env which is used to establish postgreSQL connection in my code
use below code in config/config.js to load env
import dotenv from 'dotenv'
dotenv.config()my .sequelizerc
module.exports = { 'config': path.resolve('config', 'sequelize.js'), 'models-path': path.resolve('src', 'models'), 'seeders-path': path.resolve('src', 'seeders'), 'migrations-path': path.resolve('src', 'migrations')
};and config/sequelize.js
import dotenv from 'dotenv'
dotenv.config()
export default { development: { username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, host: process.env.DB_HOST, port: process.env.DB_PORT, dialect: process.env.DB_DIALECT, dialectOptions: { bigNumberStrings: true } }, test: { username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, host: process.env.DB_HOST, port: process.env.DB_PORT, dialect: process.env.DB_DIALECT, }, production: { username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, host: process.env.DB_HOST, port: process.env.DB_PORT, dialect: process.env.DB_DIALECT, dialectOptions: { bigNumberStrings: true, } }
}; My problem was I was using "ES6 module system" and in package.json I forgot to add:
{ "type": "module", ...
}The "config.js" export was exporting the object inside "default" property because I was exporting export default configDB;
In my case I wanted the dialect being passed as a prop to a function. As a workaround I used template literals.
export function connectDB(database, dialect, logging = false) { const connectionConfig = { server: process.env[`LOCAL_${dialect.toUpperCase()}_SERVER`], database: database, username: process.env[`LOCAL_${dialect.toUpperCase()}_USERNAME`], password: process.env[`LOCAL_${dialect.toUpperCase()}_PASSWORD`], host: process.env[`LOCAL_${dialect.toUpperCase()}_HOST`], port: process.env[`LOCAL_${dialect.toUpperCase()}_PORT`], dialect: `${dialect.toLowerCase()}`, logging: logging, } return new Sequelize(connectionConfig)
}