Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS/Sequelize/MySQL - Why postgres dependencies are needed?

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.

like image 219
Mat Avatar asked Jan 27 '26 07:01

Mat


1 Answers

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.
like image 51
Ani Avatar answered Jan 28 '26 23:01

Ani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!