sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 282 forks source link

Webpack 5 "Model not initialized" issue #850

Open KAMAELUA opened 4 years ago

KAMAELUA commented 4 years ago

Versions

I'm submitting a ...

[x] bug report [ ] feature request

Actual behavior: Sequelize-typescript throwing " Model not initialized: UserModel cannot be instantiated. "UserModel" needs to be added to a Sequelize instance." exception when trying to run any queries,

The problem in model constructo: new.target is always undefined. But I can get isInitialized property from model and its set to true.

constructor(values, options) {
        if (!new.target.isInitialized) {
            throw new model_not_initialized_error_1.ModelNotInitializedError(new.target, `${new.target.name} cannot be instantiated.`);
        }
        super(values, alias_inference_service_1.inferAlias(options, new.target));
    }

It works correct if I am trying to run that code without Webpack 5. Webpack 4 works correct.

Related code: https://github.com/KAMAELUA/sequelize-typescript-webpack-error

Steps to reproduce: ts-node works correct, so everything fine with my code:

npm run start:ts

Webpack fails:

npm run buld
npm run start
chrisandrews7 commented 3 years ago

Same issue here. When downgrading to Webpack 4, it works fine.

roni-frantchi commented 3 years ago

Same here.
Anyone able to workaround it somehow?

roni-frantchi commented 3 years ago

Looking at the code output by Webpack 😨 :

class Model extends sequelize_1.Model {
    constructor(values, options) {
        if (true) {    // whaaat...
            throw new model_not_initialized_error_1.ModelNotInitializedError(new.target, `${new.target.name} cannot be instantiated.`);
        }
        super(values, alias_inference_service_1.inferAlias(options, new.target));
    }
roni-frantchi commented 3 years ago

This is a bug in Webpack 5.
Reported it here - https://github.com/webpack/webpack/issues/12339

Nothing to do with this package - I think this issue can now be resolved - upgrading to the next version of Webpack when released should do the trick.

tomfree commented 3 years ago

@roni-frantchi are you sure? According to https://github.com/webpack/webpack/pull/12343 this was merged to Master in Jan and the latest version 5.37.0 released yesterday still produces this issue as far as I can tell.

This is a bug in Webpack 5. Reported it here - webpack/webpack#12339

Nothing to do with this package - I think this issue can now be resolved - upgrading to the next version of Webpack when released should do the trick.

roni-frantchi commented 3 years ago

@tomfree haven't upgraded to 5.37.0 - we're currently on 5.36.2 and I can confirm we're not seeing this error anymore with our configuration

adamfortuna commented 2 years ago

Just came across this issue too, what fixed it for me was normalizing the paths used to import my models. In my sequelize init script i was using relative paths (./User), but in my application I was using a path alias (@models/User).

Switching it to use @models/User everywhere solved this for me.