Skip to content

Importing @sequelize/core/decorators-legacy before @sequelize/core throws "Cannot access 'Model' before initialization" #18558

Open

Description

@wikirik-agent

Issue description

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:

// CJS
require('@sequelize/core/decorators-legacy');
// ReferenceError: Cannot access 'Model' before initialization

// ESM: the import order within a file is enough
import '@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).

🤖 Generated with Claude Code

Activity

  1. added
    pending-approval
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions