'TypeORM OneToMany causes "ReferenceError: Cannot access '<Entity>' before initialization"

I have two entities: User and Habit. A user can create multiple Habits, thus I use a OneToMany relation on the User (and ManyToOne on the Habit, respectively).

User Entity

import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, BeforeInsert, BeforeUpdate, OneToMany} from "typeorm";
import * as bcrypt from "bcryptjs";
import { Habit } from "../habits/habits.entity";

@Entity()
export class User {
    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column()
    name: string;

    @Column()
    email: string;

    @Column()
    password: string;

    @OneToMany(type => Habit, habit => habit.author)
    habits: Habit[];

    @CreateDateColumn()
    dateCreated: Date;

    @UpdateDateColumn()
    dateUpdated: Date;

    @BeforeInsert()
    @BeforeUpdate()
    async hashPassword(): Promise<void> {
        this.password = await bcrypt.hash(this.password,10);
    }

    async comparePassword(password: string): Promise<boolean> {
        return bcrypt.compare(password, this.password);
    }

    constructor(props: any) {
        Object.assign(this, props);
    }
}

Habit Entity

import {Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne} from "typeorm";
import { User } from "../users/users.entity";

@Entity()
export class Habit {
    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column()
    name: string;

    @Column({ nullable: true})
    description?: string;

    @ManyToOne(type => User, user => user.habits)
    author: User;

    @CreateDateColumn()
    dateCreated: Date;

    @UpdateDateColumn()
    dateUpdated: Date;

    constructor(props: Partial<Habit>) {
        Object.assign(this, props);
    }
}

Problem

When setting up the above relation I receive the following error

WARNING in Circular dependency detected:
apps\api\src\habits\habits.entity.ts -> apps\api\src\users\users.entity.ts -> apps\api\src\habits\habits.entity.ts

WARNING in Circular dependency detected:
apps\api\src\users\users.entity.ts -> apps\api\src\habits\habits.entity.ts -> apps\api\src\users\users.entity.ts


/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "User", function() { return User; });
                                                                                               ^
ReferenceError: Cannot access 'User' before initialization
    at Module.User (...\dist\apps\api\main.js:1782:96)
    at Module../apps/api/src/habits/habits.entity.ts (...\dist\apps\api\webpack:\apps\api\src\habits\habits.entity.ts:42:13)
    at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
    at Module../apps/api/src/users/users.entity.ts (...\dist\apps\api\main.js:1790:79)
    at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
    at Module../apps/api/src/config/db-config.service.ts (...\dist\apps\api\main.js:1038:77)
    at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
    at Module../apps/api/src/config/config.module.ts (...\dist\apps\api\main.js:978:76)
    at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
    at Module../apps/api/src/app/app.module.ts (...\dist\apps\api\main.js:147:79)

Note

I use Nx and have created a NestJS app. The TypeOrm version is "^0.2.22" and the @nestjs/typeorm version is "^6.2.0"

My tsconfig is as follows:

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@awhile/contracts": ["libs/contracts/src/index.ts"],
      "@awhile/ui": ["libs/ui/src/index.ts"]
    }
  },
  "exclude": ["node_modules", "tmp"]
}

I have tried to use a ManyToMany relation and it worked. Also, on a separate NestJS app (without Nx) I cannot reproduce this reference error. Changing the ECMAScript target version in the tsconfig.json also did not work.

Both entities are only used in their services and are not instantiated anywhere else.

I appreciate any help. Thank you in advance.



Solution 1:[1]

I solved this by using autoLoadEntities: true when loading the TypeORM config to NestJS. Note that this is a NestJS extra, so if you are using ormconfig.json this property won't be applied.

autoLoadEntities documentation here: https://docs.nestjs.com/techniques/database#auto-load-entities

UPDATE 04/10/2020

I kept having the same issue with relations. Another solution I found, even if it breaks some standards is to add all the Entities into 1 file. Export them there and import them where needed.

Keep in mind that the order in which the classes are declared matters.

Solution 2:[2]

In tsconfig, setting the target to "esnext" and module to "commonjs" fixed the issue for me

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "types": ["node", "jest"],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "target": "esnext"
  },
  "include": ["**/*.ts"]
}

Solution 3:[3]

You can accomplish the same thing as @jdruper by using a barrel file. If you don't have a paths property in your tsconfig.json, add one under compilerOptions that looks something like this:

"paths": {
   "@entities":["app/src/entities/index.ts"]
}

Put all your entity classes in the entities folder, create the index.ts file and add export statements in the index.ts file:

export * from './user.entity';
export * from './habit.entity';

Make sure the export statements appear in the required order. Your imports would then look like this.

import { User, Habit } from '@entities';

You can search for typescript barrel file for more information on how to work with barrel files.

If you're using NX you can make an entities lib and accomplish the same thing.

It will probably mean having to move all your entity files together and updating the imports in your modules and services. Maybe not exactly ideal, but I would prefer that over having them all in one file.

Solution 4:[4]

Use the Relation type wrapper to avoid circular dependency issues, as described here.

For example:

import {Entity, OneToMany, Relation} from "typeorm";
import {Habit} from "../habits/habits.entity";

@Entity()
export class User {
    ...

    @OneToMany(type => Habit, habit => habit.author)
    habits: Relation<Habit>[];

    ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 SeanH
Solution 4 theuser