sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Webpack 5 "Model not initialized" issue

Open KAMAELUA opened this issue 4 years ago • 7 comments

Versions

  • sequelize: 5.21.10
  • sequelize-typescript: 1.1.0
  • typescript: 4.0.5
  • webpack: 5.4.0

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

KAMAELUA avatar Nov 03 '20 16:11 KAMAELUA

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

chrisandrews7 avatar Nov 17 '20 13:11 chrisandrews7

Same here.
Anyone able to workaround it somehow?

roni-frantchi avatar Jan 05 '21 10:01 roni-frantchi

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 avatar Jan 05 '21 11:01 roni-frantchi

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.

roni-frantchi avatar Jan 06 '21 08:01 roni-frantchi

@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.

tomfree avatar May 11 '21 13:05 tomfree

@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

roni-frantchi avatar May 11 '21 15:05 roni-frantchi

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.

adamfortuna avatar Nov 17 '21 20:11 adamfortuna