48

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
6
  • Did you look at sequelize variable at the line const model = sequelize.import(path.join(__dirname, file)) using a breakpoint?
    – Anatoly
    Jul 15, 2020 at 17:48
  • Seems like a syntax error. Follow the suggestion of @Anatoly and use breakpoints to track code flow. If you can't find share the whole code of the file.
    – Mansur
    Jul 16, 2020 at 7:55
  • 1
    how did you fix this, I have the same issue?? Jul 22, 2020 at 11:21
  • 1
    I haven't found a solution yet @bihireboris . I'm new to stack overflow and hoping someone to help. Jul 22, 2020 at 11:29
  • Aight, will let know when I have some progress. keep up the learning Jul 22, 2020 at 11:34

7 Answers 7

121

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.

9
  • 2
    Any idea what version this was removed? The sequelize guys really don't give a crap about breaking a known api, huh? (sigh)
    – ChrisH
    Nov 14, 2020 at 19:25
  • 1
    @ChrisH I'm pretty sure it's version 6 and forward, but I have a vague memory seeing this somewhere in late version 5. Nov 19, 2020 at 4:57
  • 2
    It was removed from v5 -> v6: refactor: remove sequelize.import helper #12175. Docs: Upgrade to v6. @DavidKamer I'm sorry, I don't understand how to change the sequelize.import() code into a v6 working code...
    – Chris
    Jan 3, 2021 at 23:58
  • 1
    require isn't supported in esm. Mar 31, 2021 at 0:17
  • 3
    @GregoryBologna if you mean your start script is being loaded with esm like "start": "node -r esm app.js", you need to remove "type": "module" from your package.json specifications and you will be fine. This is because specifying type of module implies you can't use require anywhere even with esm. Once that is removed, esm will compile your code aptly. Apr 8, 2021 at 17:31
16

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

2
  • Do you have the source where you found this? Jan 2 at 15:37
  • sequelize.define(path.join(__dirname, file)) Feb 7 at 8:30
10

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

6
3

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);

1
  • Your answer could be improved by adding more information on what the code does and how it helps the OP.
    – Tyler2P
    Jan 23, 2022 at 20:31
1
  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.

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 9, 2022 at 22:14
  • Oh god! thak you for this answer, it's a mistake dificult to find out the why. I had an empty file in models folder, after removing it, everything works fine. Jan 26, 2022 at 16:20
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) => {
    ...
}
-1

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.

2
  • 1
    Those two code snippets are exactly the same!
    – SunnyPro
    Feb 18 at 1:18
  • They're not! In the first one, password is object with a key of type. In the second one, password is a String. Mar 12 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.