You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Importing @sequelize/core/decorators-legacy before @sequelize/core throws "Cannot access 'Model' before initialization" #18558
If @sequelize/core/decorators-legacy is the first Sequelize module loaded, loading fails with a TDZ error. It happens with both CommonJS and ESM. If @sequelize/core is loaded first, everything works.
Reproduced on main (cfba176dd, 7.0.0-alpha.48) with Node 24:
// CJSrequire('@sequelize/core/decorators-legacy');// ReferenceError: Cannot access 'Model' before initialization// ESM: the import order within a file is enoughimport'@sequelize/core/decorators-legacy';import'@sequelize/core';// ReferenceError: Cannot access 'Model' before initialization
An app can hit this when the first file it loads imports only from decorators-legacy, for example a shared decorators/helpers module, or a model file whose imports are ordered differently from what prettier produces.
Cause
This is the load chain when decorators-legacy is the entry point (built lib/ output):
decorators/legacy/index.js
→ decorators/legacy/associations.js
→ associations/index.js
→ associations/belongs-to.js
→ associations/has-one.js
→ model.js (starts loading, Model not yet initialized)
→ model-typescript.js
→ sequelize.js (runs top-level code)
Sequelize.Model = Model; ← packages/core/src/sequelize.js:825 reads Model while model.js is still loading
When @sequelize/core is the entry point, model.js finishes before sequelize.js reaches that line, so nothing breaks.
Possible fixes
Make the static re-exports on Sequelize (Sequelize.Model, and probably the association classes next to it) lazy getters, or set them from index after everything has loaded.
Or have decorators/legacy/index load the main entry first, so it is always the one that triggers module loading.
The second option is the smaller change. The first removes the eager read that causes the crash.
Found while reviewing #18234; a static import of Model in utils/model-utils.ts crashes in a similar way (Cannot access 'MultiAssociation' before initialization).
Activity