sequelize: Error in sequelize.import: defineCall is not a function
What you are doing?
I just had this line in my bin/www require('../models')
this load the index.js (seen below).
The it generated an error.
File structure in the models folder: models |- index.js |- user.js
index.js:
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require('../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(process.env.MYSQL_DATABASE , process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
host: process.env.MYSQL_HOST || 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
}
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;
user.js:
module.export = function(sequelize, DataTypes)
{
return sequelize.define('users', {
username: {type: DataTypes.STRING, primaryKey: true},
password: DataTypes.STRING,
stamm: DataTypes.STRING,
lv: DataTypes.String,
countSipplinge: DataTypes.INT,
avgAge: DataTypes.INT,
sifüName: DataTypes.STRING,
sifüEMail: DataTypes.STRING,
sifüAdresse: DataTypes.STRING,
sifüTel: DataTypes.STRING,
email: DataTypes.STRING,
admin: DataTypes.BOOLEAN,
sippe: DataTypes.BOOLEAN,
inactive: DataTypes.BOOLEAN
})
};
What do you expect to happen?
I expected to load the File and Model user.js as seen below:
What is actually happening?
But just run into an error:
C:\Users\Jan\WebstormProjects\BdPWebanwendung\node_modules\sequelize\lib\sequelize.js:691
this.importCache[path] = defineCall(this, DataTypes);
^
TypeError: defineCall is not a function
at Sequelize.import (C:\Users\Jan\WebstormProjects\BdPWebanwendung\node_modules\sequelize\lib\sequelize.js:691:30)
at C:\Users\Jan\WebstormProjects\BdPWebanwendung\models\index.js:32:33
at Array.forEach (native)
at Object.<anonymous> (C:\Users\Jan\WebstormProjects\BdPWebanwendung\models\index.js:31:4)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Jan\WebstormProjects\BdPWebanwendung\config\passport.js:5:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
I checked with an debugger where it actually happens. The error is generated by the following line:
if (!this.importCache[path]) {
var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
if (typeof defineCall === 'object') {
// Babel/ES6 module compatability
defineCall = defineCall.default;
}
this.importCache[path] = defineCall(this, DataTypes);
}
defineCall is undefined and it was set by the line defineCall = defineCall.default;
.
In this line is defineCall the object of require('path/to/user.js')
but defineCall.default
is undefined
.
Dialect: mysql Database version: 5.7.12 Sequelize version: 3.27.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 26 (3 by maintainers)
That means you have tried to import a module that did not do a
module.exports = function() {
I experienced this issue, the reason was because I added a file on my models directory that is not a model, just some code that’s meant to do some stuff. I suppose we can only add model files on the models directory, as
index.js
checks every file in the whole directory.Can we edit the codes in the
index.js
to look forfilename.model.js
instead of every file or maybe make it configurable withmodel
as the default?There is a spelling mistake in
user.js
, you need themodule.exports
but you actually havemodule.export
😛This is a CLI issue and must be fixed. This occurs when you make a module.exports in a object like module.exports = {…}. If you make module.exports = () =>{} the error gone but the Sequelize cannot import you module. To correct this bug declare you models as:
module.exports = () => { return { username: {type: DataTypes.STRING, primaryKey: true}, password: DataTypes.STRING, stamm: DataTypes.STRING, lv: DataTypes.String, countSipplinge: DataTypes.INT, avgAge: DataTypes.INT, sifüName: DataTypes.STRING, sifüEMail: DataTypes.STRING, sifüAdresse: DataTypes.STRING, sifüTel: DataTypes.STRING, email: DataTypes.STRING, admin: DataTypes.BOOLEAN, sippe: DataTypes.BOOLEAN, inactive: DataTypes.BOOLEAN } }
In my case, it was the
index.js
file created inside mymodels
directory bysequelize CLI
when i ran itsinit
command. After simply removing this file, the error went away. This still seems like a conflicting behavior between the CLI and how sequelize imports models; and I’d like to learn why this is so – or, if it’s more of poor incompatibility issue between diff versions ofsequelize
&postgres
and other accompanying tools. If anyone has more insight, please kindly do let me know here.Okay. This fixed it for me, was caused by an empty file in the models folder. Sequelize is trying to import a file that isn’t a model.
You might need to delete old models on the dist folder if you are using babel
I found this solution: Replace this line: const model = sequelize.import(path.join(__dirname, file)); By: const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
Hi everyone, as a side note,
sequelize.import
will probably be deprecated soon (of course the CLI will be updated accordingly). Just so you know.I also had this issue, and for me it was caused by a file in the models folder that I had completely commented out - and was redundant. After I deleted the file, things continued as normal.
I’m experiencing this issue as well right now, i tried to put console.log in the index.js to check that sequelize is really imported, but the log didn’t show up at all, is it normal that index.js in the model was seemed skipped or something was wrong?