question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in sequelize.import: defineCall is not a function

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

49reactions
felixfbeckercommented, Nov 19, 2016

That means you have tried to import a module that did not do a module.exports = function() {

25reactions
aprilmintacpinedacommented, Jan 31, 2018

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 for filename.model.js instead of every file or maybe make it configurable with model as the default?

Read more comments on GitHub >

github_iconTop Results From Across the Web

defineCall is not a function, sequelize error - Stack Overflow
The error is, as I understand it generated because I'm importing something that's not a sequelize model to my index.js file (see below...
Read more >
[Solved]-defineCall is not a function, sequelize error-sequelize.js
In the model that you have specified it exports a model definition and that is the reason for error. sequelize expects every model...
Read more >
Node.js – Error on sequelize.import: defineCall is not a function
The error I'm getting is this: C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392 this.importCache[path] = defineCall(this, ...
Read more >
defineCall is not a function at Sequelize.import sequelize.js
//BASICALLY DON'T ADD EMPTY FILES TO YOUR MODELS FOLDER /* I experienced this issue, the reason was because I added a file on...
Read more >
Sequelize TypeError: defineCall is not a function - Treehouse
After doing some research, it appears that this could be caused when trying to import a non-sequelize object. Below is the problematic index.js ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found