TypeError: defineCall is not a function. require() fails

TypeError: defineCall is not a function. require() fails

我最近加入了一个使用 Sequelize 构建数据库模型的项目。但是,在获得 TypeError: defineCall is not a function 之前,我无法使用 sequelize.import 导入多个文件。使用 require() 导入某些文件的默认函数似乎有错误。我有以下结构:

models/index.ts

import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';

const sequelize = new Sequelize('mysql://root:password@localhost/myDatabase', {
    dialect: 'mysql',
    logging: false
});

const db: any = {};

fs.readdirSync(__dirname).filter(file => {
    return (file.indexOf('.') !== 0) && (file !== 'index.ts') && (file.endsWith('.js'));
}).forEach(file => {
    const model = sequelize.import(path.join(__dirname, file)); //<<<<<<<<<< This brings an error
    db[model.name.charAt(0).toUpperCase() + model.name.slice(1)] = model;
});

失败的地方:

sequelize.js

  import(path) {
    // is it a relative path?
    if (Path.normalize(path) !== Path.resolve(path)) {
      // make path relative to the caller
      const callerFilename = Utils.stack()[1].getFileName();
      const callerPath = Path.dirname(callerFilename);
      path = Path.resolve(callerPath, path);
    }

    if (!this.importCache[path]) {
      let defineCall = arguments.length > 1 ? arguments[1] : require(path);

      if (typeof defineCall === 'object') {
        // ES6 module compatability
        defineCall = defineCall.default;
      }

      this.importCache[path] = defineCall(this, DataTypes);// <<<<<<<< defineCall is undefined here
    }

    return this.importCache[path];
  }

导致未定义的 defineCall 的典型文件:

models/action-calls.js

export default function(sequelize, DataTypes) {
    return sequelize.define('actionCalls', {
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
        actionId: { type: DataTypes.INTEGER, allowNull: false },
        userId: { type: DataTypes.INTEGER, allowNull: false },
        date: { type: DataTypes.DATE, allowNull: false },
        number: { type: DataTypes.STRING(32), allowNull: false },
        duration: { type: DataTypes.INTEGER }
    }, {
        tableName: 'ActionCalls',
        timestamps: false
    });
}

奇怪的是,有时导入此文件没有问题,但随后另一个文件最终出现错误。我查看了 models/ 文件夹中的所有文件,它们都遵循与 action-calls.js 相同的模式。直到上周它第一次出现时,我才遇到这个问题。我的同事没有遇到相同代码库的错误。我试过回到以前的提交,但错误现在仍然存在。任何帮助将不胜感激。


原来是ts-node-dev的问题。将它从 1.0.0-pre.49 更新到 1.1.6 后,错误消失了。