Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

476
Vistas
sequelize.import is not a function

I'm new to express.

I want to import files to sequelize and declared:

const model = sequelize.import(path.join(__dirname, file))

                             ^

It returned the following type error

TypeError: sequelize.import is not a function

And then, edited code to

var model = require(path.join(__dirname, file))(sequelize, Sequelize);

                                         ^

Then the error is:

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

I think it is the error in importing stuff....

Here is my whole file code:

const fs = require('fs');

const path = require('path');

const Sequelize = require('sequelize');

const config = require('../config/config');

const db = {}

var __dirname = path.resolve();


const sequelize = new Sequelize(
    config.db.database,
    config.db.user,
    config.db.password,
    config.db.options
)


fs

    .readdirSync(__dirname)

    .filter((file) =>
        file !== 'index.js'
    )

    .forEach((file) => {

        //const model = sequelize.import(path.join(__dirname, file))

        var model = require(path.join(__dirname, file))(sequelize, 
Sequelize);

        db[model.name] = model

    })


db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db
over 4 years ago · Santiago Trujillo
7 Respuestas
Responde la pregunta

0

  1. Check that you do not have files in models directory that are not exporting anything.
  2. Check that your exports are functions with same parameters as the instance it is being called in the require statement.

In any case like mine, I had empty files in my models directory. require(...) is not a function simply because the index logic is iterating over and importing files that do not export anything from the models directory.

over 4 years ago · Santiago Trujillo Denunciar

0

in my case i replaced this code

const model = require(path.join(__dirname, file))( sequelize, Sequelize.DataTypes );

with this :

const model = require(path.join(__dirname, file)).default(sequelize, Sequelize.DataTypes);

over 4 years ago · Santiago Trujillo Denunciar

0

People who tell downgrading not worked: Watch out for your index.js file!

For me downgrading to ^5.22.3 and changing

module.exports = (sequelize, DataTypes) => {
    sequelize.define('User', {
        email: {
            type: DataTypes.STRING,
            unique: true
        },
        password: {
            type: DataTypes.STRING
        }
    })

}

to

module.exports = (sequelize, DataTypes) =>
    sequelize.define('User', {
        email: {
            type: DataTypes.STRING,
            unique: true
        },
        password: DataTypes.STRING
    })

worked. That was the problem for me.

over 4 years ago · Santiago Trujillo Denunciar

0

This might help someone else out there, in version 6.6.5 it's deprecated and you should replace it with sequelize.define.

over 4 years ago · Santiago Trujillo Denunciar

0

As of now I was able to fix the issue by downgrading the sequelize module version in your package.json to "sequelize": "^5.22.3",. do let me know if it is also fixed on your side.

Edit: any sequelize version under < 6.0.0 should work as normal

over 4 years ago · Santiago Trujillo Denunciar

0

The error is caused by using the sequelize import object. Instead you should use Node's built in CommonJS require function. So change this line in your models/index.js:

const model = sequelize['import'](path.join(__dirname, file))

to:

const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes)

You can also just regenerate the models directory and readd your models without the old index.js if you find that easier:

mv models models.bak && sequelize init:models && mv models.bak/index.js models.bak/index.js.bak && mv models.bak/* models/ && rm models.bak

That one liner will fix your problem if you have sequelize-cli installed globally. If you don't you can use this one:

npm i --save-dev sequelize-cli && mv models models.bak && npx sequelize init:models && mv models.bak/index.js models.bak/index.js.bak && mv models.bak/* models/ && rm models.bak

You may also need to update your config folder. I use a JavaScript config to inject ENVs, so I had to add to change my const config = require(... line to reflect that. If you used one of my one liners your old models/index.js file is now at index.js.bak if you need to grab any custom stuff from it.

over 4 years ago · Santiago Trujillo Denunciar

0

Check if you have exported the model using old JavaScript syntax.

In my case, it was enough to change the code from:

export default (sequelize, DataTypes) => {
    ...
}

to

module.exports = (sequelize, DataTypes) => {
    ...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda