cli icon indicating copy to clipboard operation
cli copied to clipboard

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

Open OhjeezwhathaveIdone opened this issue 4 years ago β€’ 23 comments

Issue Description

'Error: Dialect needs to be explicitly supplied as of v4.0.0' is thrown when initialising sequelize via a destructured property whilst running 'npx sequelize-cli db:migrate'.

What are you doing?

I have a config.js which merges different sections of a config.json dependent on the environment so as to prevent repeated configurations, it exports the final, merged, configuration. Within the final configuration is a sub object of 'dev' called 'database'.

Sequelize's .sequelizerc is unable to process this as the config paramter expects a file path, not an object and thus is not using the 'database' object and instead is using the whole object. When trying to run 'npx sequelize-cli db:migrate' the error above is thrown.

If I change config.js to instead export 'finalConfig.database', 'npx sequelize-cli db:migrate' works without issue.

What do you expect to happen?

For 'npx sequelize-cli db:migrate' to migrate successfully.

What is actually happening?

An error is thrown.

Sequelize CLI [Node: 15.5.1, CLI: 6.2.0, ORM: 6.3.5]

Loaded configuration file "config\config.js".

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

Additional context

config/config.json

{
  "dev": {
    "database": {
      "name": "name",
      "host": "host",
      "username": "user",
      "password": "pass",
      "dialect": "postgres"
    }
  },
  "live": {
    "database": {
      "name": "name1",
      "username": "username1",
      "password": "password1"
    }
  }
}

Works config/config.js

const _ = require('lodash');
const config = require('./config.json');
const defaultConfig = config.dev;
const environment = process.env.NODE_ENV || 'dev';
const environmentConfig = config[environment];
const finalConfig = _.merge(defaultConfig, environmentConfig);

module.exports = finalConfig.database;

Does not work config/config.js

module.exports = finalConfig;

Environment

  • Sequelize version: 6.3.5
  • Node.js version: 15.5.1
  • Operating System: Windows 10

Issue Template Checklist

How does this problem relate to dialects?

  • [ ] I think this problem happens regardless of the dialect.
  • [ ] I think this problem happens only for the following dialect(s):
  • [x] I don't know, I was using postgres, with connector library version 8.5.1 and database version 11.6 (ElephantSQL)

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.
  • [ ] No, I don't have the time, although I believe I could do it if I had the time...
  • [ ] Yes, I have the time but I don't know how to start, I would need guidance.
  • [x] No, I don't have the time and I wouldn't even know how to start.

OhjeezwhathaveIdone avatar Jan 15 '21 00:01 OhjeezwhathaveIdone

I lied when I said I didn't have time and wouldn't even know how to start, I wouldn't let this beat me.

I've created a pull request, as above, that will allow objects to be exported from .sequelizerc.

In my case, and with the pull request, the below .sequelizerc now meets my expected outcome:

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

module.exports = {
  'config': database,
  'models-path': path.resolve('./models'),
  'seeders-path': path.resolve('./seeders'),
  'migrations-path': path.resolve('./migrations')
};

OhjeezwhathaveIdone avatar Jan 16 '21 00:01 OhjeezwhathaveIdone

Use this export NODE_ENV=development

Asharudheen-VNC avatar Jun 21 '21 13:06 Asharudheen-VNC

Hello, @Asharudheen-VNC , i have the same issue, where do I use your suggestion ?

OGUNSOLA avatar Jul 12 '21 11:07 OGUNSOLA

Making use of .sequelizerc file , which am currently having have not solved the problem. Thank you for making a pull request on this

iamgabrielsoft avatar Jul 15 '21 03:07 iamgabrielsoft

@iamgabrielsoft I have the same problem

rubenaprikyan avatar Oct 01 '21 21:10 rubenaprikyan

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. πŸ™‚

github-actions[bot] avatar Oct 28 '21 00:10 github-actions[bot]

Not stale.

OhjeezwhathaveIdone avatar Oct 31 '21 00:10 OhjeezwhathaveIdone

Some news? I have the same issue when running with pm2. Running with npm start ou nodemon works fine. Some clue?

moigamijunior avatar Nov 01 '21 12:11 moigamijunior

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. πŸ™‚

github-actions[bot] avatar Nov 16 '21 00:11 github-actions[bot]

Check project .env file if you are using environment variables, I forgot to set dialect after setting-up .env file properly its working fine

Amit-Dagar avatar Apr 19 '22 18:04 Amit-Dagar

I had this issue as well, somewhere sequelize is looking for a development environment, once i added development this error went away. My config file now shows development, test, and production options.

bastianhilton avatar Jul 24 '22 06:07 bastianhilton

I had this issue as well, somewhere sequelize is looking for a development environment, once i added development this error went away. My config file now shows development, test, and production options.

The default environment that the CLI uses is development. This can be changed in the .env file

WikiRik avatar Jul 24 '22 10:07 WikiRik

Tentei todas essas opΓ§Γ΅es e o erro permanece, o que poderia tentar?

rosalyneduarte avatar Aug 01 '22 20:08 rosalyneduarte

Esse erro sumiu depois de colocar esse trecho no index.js

const dbConfig = require("../config/db.config")['development']

rosalyneduarte avatar Aug 01 '22 21:08 rosalyneduarte

Hello, Does someone knows how to make the import version of this: "const dbConfig = require("../config/db.config")['development']" ??

omargarciagiorgini avatar Dec 03 '22 18:12 omargarciagiorgini

Arquivo config.js { "development": { "database": { "name": "name", "host": "host", "username": "user", "password": "pass", "dialect": "postgres" }, "dev": { "database": { "name": "name", "host": "host", "username": "user", "password": "pass", "dialect": "postgres" } }, "live": { "database": { "name": "name1", "username": "username1", "password": "password1" } } }

Arquivo .env NODE_ENV= development, "name": "name", "host": "host", "username": "user", "password": "pass", "dialect": "postgres"

rosalyneduarte avatar Dec 03 '22 20:12 rosalyneduarte

Hello, Does someone knows how to make the import version of this: "const dbConfig = require("../config/db.config")['development']" ??

Same issue

Cyebukayire avatar Mar 15 '23 11:03 Cyebukayire

Basically in this issue Sequelize cannot find the environment on which it have to perform action. By default sequelize look for value of NODE_ENV like if you have set "development" in your NODE_ENV , sequelize will look for "development" in object define in config.js file. In your case set (in .env file) NODE_ENV='dev' or NODE_ENV="dev" if you want to configure dev write above in your .env file else set the required environment in NODE_ENV.

in .env file

NODE_ENV= development

in config.js

require('dotenv').config() module.exports={ "development": { "username": process.env.DATABASE_USER, "password": process.env.DATABASE_PASSWORD, "database": process.env.DATABASE_NAME, "host": process.env.DATABASE_HOST, "dialect": 'postgres', }, }

suhaibkhawaja2000 avatar May 19 '23 09:05 suhaibkhawaja2000

Basically in this issue Sequelize cannot find the environment on which it have to perform action. By default sequelize look for value of NODE_ENV like if you have set "development" in your NODE_ENV , sequelize will look for "development" in object define in config.js file. In your case set (in .env file) NODE_ENV='dev' or NODE_ENV="dev" if you want to configure dev write above in your .env file else set the required environment in NODE_ENV.

in .env file

NODE_ENV= development

in config.js

require('dotenv').config() module.exports={ "development": { "username": process.env.DATABASE_USER, "password": process.env.DATABASE_PASSWORD, "database": process.env.DATABASE_NAME, "host": process.env.DATABASE_HOST, "dialect": 'postgres', }, }

thanks bro, it's work for me use typescript in express js

maybe some one need, this my config/db.ts

`import dotenv from 'dotenv' import { Dialect, Sequelize } from 'sequelize'

dotenv.config()

let dbName = process.env.database_production let username = process.env.username_production let password = process.env.password_production let dbHost = process.env.host_production let dbDriver = process.env.dialect_production

const env = process.env.NODE_ENV if (env === 'development') { dbName = process.env.database_development username = process.env.username_development password = process.env.password_development dbHost = process.env.host_development dbDriver = process.env.dialect_development }

const db = new Sequelize(dbName as string, username as string, password, { host: dbHost, dialect: dbDriver as Dialect, })

export default db `

sanryuu03 avatar Jul 22 '23 03:07 sanryuu03

Basically in this issue Sequelize cannot find the environment on which it have to perform action. By default sequelize look for value of NODE_ENV like if you have set "development" in your NODE_ENV , sequelize will look for "development" in object define in config.js file. In your case set (in .env file) NODE_ENV='dev' or NODE_ENV="dev" if you want to configure dev write above in your .env file else set the required environment in NODE_ENV.

in .env file

NODE_ENV= development

in config.js

require('dotenv').config() module.exports={ "development": { "username": process.env.DATABASE_USER, "password": process.env.DATABASE_PASSWORD, "database": process.env.DATABASE_NAME, "host": process.env.DATABASE_HOST, "dialect": 'postgres', }, }

Just had this same issue. Resolved by following this steps.

Thanks!

filipeaguiars avatar Sep 29 '23 22:09 filipeaguiars

I did all of those solutions you suggested, but none working.😭😭

valens200 avatar Feb 06 '24 13:02 valens200

I did all of those solutions you suggested, but none working.😭😭

Can you explain more what is your issue? On the other note in recent vs code update its not updating the the .env values when we edit .env file , somehow every time it use the perviously assigned value even though project or vs code is restarted. Don't know why. its happening but its occurring only locally not on server. In thais case we need to use source .env in vs code terminal to configure the new values every time before running the project locally .

suhaibkhawaja2000 avatar Feb 08 '24 06:02 suhaibkhawaja2000