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.
mysql2
dialect though and notmysql
since you're using themysql2
interface.console.log(config)
inmodels/index.js
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 theconfig.json
. Am I right?