'const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes) - TypeError: require(...) is not a function

I am beginner in web dev and trying to create a project using Mysql, Node.js with Express. Facing this TypeError issue while using Sequalize. Can someone please explain it to me and help me find a solution?

"sequelize": "^6.19.0"

"sequelize-cli": "^6.4.1"

/models/user.js:

module.exports = (sequelize, DataTypes) => {
  const user = sequelize.define('User', {
    name: DataTypes.STRING,
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
  });

  return user;
};

/models/index.js:

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(`${__dirname}/../../config/config.js`)[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter((file) => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

error:

[nodemon] starting `node ./src/api/server.js`
/home/back-end/src/models/index.js:25
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
                                                     ^

TypeError: require(...) is not a function
    at /home/back-end/src/models/index.js:25:54
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/back-end/src/models/index.js:24:4)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/back-end/src/services/usersService.js:1:18)
[nodemon] app crashed - waiting for file changes before starting...


Solution 1:[1]

From experience, I encountered this after I added a file that is not a model in the 'models' directory. Supposedly one can only add model files to the 'models' directory, as index.js checks every file in the whole directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 leaj_araf