3

I'm new to Node and Sequelize and I'm facing a problem with establishing connection to MySQL database. I wonder why Postgres dependencies are needed even if set the dialect to mysql one. I would be grateful if you can help me to point the problem.

From packages.json

"dependencies": {
    // ...
    "mysql2": "1.1.2",
    "sequelize": "3.27.0"
},

The way I'm trying to connect db:

var Sequelize = require('sequelize');

var sequelize = new Sequelize('dbName', 'dbUser', 'dbPass', {
    host: 'localhost',
    dialect: 'mysql'
});

sequelize.authenticate()
    .then(function(err) {
        console.log('Connection has been established successfully.');
    })
    .catch(function (err) {
        console.log('Unable to connect to the database:', err);
    });

The error I'm facing:

Failed to compile.

Error in ./~/sequelize/lib/dialects/postgres/hstore.js
Module not found: 'pg-hstore' in ~/node_modules/sequelize/lib/dialects/postgres
@ ./~/sequelize/lib/dialects/postgres/hstore.js 3:13-33


UPDATE 1 (sequelize init command used)

From models/index.js:

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(module.filename);
var env       = 'development'; //process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/../config/config.json')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(function(file) {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

And my config/config.json:

{
  "development": {
    "username": "dbUser",
    "password": "dbPassword",
    "database": "dbName",
    "host": "127.0.0.1",
    "dialect": "mysql2"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql2"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql2"
  }
}


UPDATE 2

Now I see I have an additional message:

Error: The dialect mysql is not supported. (Error: Cannot find module './mysql'.)

However, I have it installed in to root of the node_modules directory.


UPDATE 3

The steps I take to reproduce the problem:

  • create-react-app tmpApp
  • npm install --save mysql sequelize
  • npm install -g sequelize-cli
  • sequelize init
  • add to src/index.js: var models = require('../models')
  • npm start - the problem appears


UPDATE 4 - Problem resolved

First of all really thanks for your help! The problem actually occurred because I was trying to mix server and client sides into one application (I was just playing). The Sequelize and the mysql modules itself do not seem to work on the browser environment. When the server side is placed outside the react-app everything behaves as expected.

5
  • You shouldn't need the postgres module. Make sure that you're using the mysql2 dialect though and not mysql since you're using the mysql2 interface. Commented Nov 25, 2016 at 9:01
  • what is the value you get when you do console.log(config) in models/index.js Commented Nov 25, 2016 at 11:06
  • @AJS, the config.use_env_variable is undefined, what seems to be reasonable to me, since I don't want to use configuration taken from environmental variable but from the config.json. Am I right? Commented Nov 25, 2016 at 11:09
  • yes it is what about config variable what is its output Commented Nov 25, 2016 at 11:11
  • I've updated my question Commented Nov 25, 2016 at 11:43

2 Answers 2

1

In your database config, you must have set the config as something else to be used.

  1. Check the env using sequelize. Usually, it's a config.json file inside your database directory.
  2. Change the dialect to mysql2 since that's what you want to use.
Sign up to request clarification or add additional context in comments.

6 Comments

My project was pretty empty, so I've just used sequelize-cli init command and than modified config/config.json. Now I also use the sequelize instance from models/index.js, but the problem remains. Dialect of course changed to mysql2.
Show your models/index.js It should by default use the development config from config.json Otherwise it's defaulted to use postgres.
@Anirudha if you change to dialect mysql2 it willl give you following error: Supported dialects: mariadb, mssql, mysql, postgres, and sqlite.
@Mat You need to install these dependencies. npm install --save mysql
@Anirudha, actually I have it. In fact I've found the problem - please take a look at my updated question.
|
-1

To solve your problem do:

`npm install mysql ---save` instead of `mysql2`

that will get your code working. And from what i saw in offical docs here they seem to support mysql2 as well but it doesn't work. As i have tested and got similar results. I will suggest raise a issue in their github so they can fix it their. You can do so here

7 Comments

Unfortunately, I've already tried both of them with the same result: Error: The dialect mysql is not supported. (Error: Cannot find module './mysql'.). or Error: The dialect mysql2 is not supported. (Error: Cannot find module './mysql2'.) Of course I have them installed.
it worked when i tried it why not remove both and just install mysql again, you can do so by npm uninstall <module_name> --save
keep dialect as mysql not mysql2
Dialect set to mysql and npm packages: "mysql": "2.12.0", "sequelize": "2.0.0". The problem remains though.
is your code exactly shown in your post? ie. file where you are creating connection?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.