Circular import error with sequelize and Esmodules

319 Views Asked by At

i'm working on a project with tinyHttp V2 which uses only EsModules.

I'm trying to make a sample project with some sequelize models but i'm not able to start anything, typescript compilation is complaining about circular import inside models (which was fine with commonJs),

Here is the error i get :

yarn run v1.22.17 $ node --experimental-specifier-resolution=node --experimental-modules --loader ts-node/esm ./sequelize/seed/seeder (node:10691) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time (Use node --trace-warnings ... to show where the warning was created) ReferenceError: Cannot access 'Project' before initialization at file:///.../test/sequelize/models/Task.ts:32:14 at ModuleJob.run (internal/modules/esm/module_job.js:170:25) at async Loader.import (internal/modules/esm/loader.js:178:24) at async Object.loadESM (internal/process/esm_loader.js:68:5) error Command failed with exit code 1.

and this is my TsConfig

{
  "ts-node": {
    "esm": true
  },
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ESNext"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    /* Modules */
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    /* JavaScript Support */
    "allowJs": true,
    /* Emit */
    "declaration": true,
    "outDir": "dist",
    "downlevelIteration": true,
    "preserveConstEnums": true,
    "isolatedModules": false,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "preserveSymlinks": true,
    "forceConsistentCasingInFileNames": true,
    /* Type Checking */
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "noUnusedParameters": false,
    /* Completeness */
    "skipDefaultLibCheck": true,
    "skipLibCheck": true
  },
  "include": ["*.ts", "**/*.ts"],
  "exclude": ["packages/*/dist", "node_modules"]
}

i tried to change module resolution but with no luck, i'm stuck, any idea ?

1

There are 1 best solutions below

0
On

I succeeded to get this working, the solution was found in typeOrm code i had to define this type

export type CircularHelper<T> = T

and then in models

@Table({
    tableName: 'user',
    underscored: true,
    paranoid: true,
    timestamps: true
})
export class User extends Model{

    @Column
    firstname: string

    @Column
    lastname: string

    @Column
    password: string

    @Column
    email: string

    @HasMany(()=> UserProject)
    projects: CircularHelper<UserProject>[]

    @HasMany(()=> Task)
    tasks: CircularHelper<Task>[]

}
@Table({
    tableName: 'task',
    paranoid: false,
    timestamps: true,
    underscored: true
})
export class Task extends Model{
    @Column
    title: string

    @Column(DataType.TEXT)
    body: string

    @Column
    completion: number

    @ForeignKey(()=> User)
    @Column
    userId: number

    @BelongsTo(()=> User)
    user: CircularHelper<User>

    @ForeignKey(()=>Project)
    @Column
    projectId: number
    @BelongsTo(()=>Project)
    project: CircularHelper<Project>

}

I hope it will help someone