When using the Nest CLI, I can define bidirectional relationships such as:
menu.entity.ts
@ManyToOne((type) => Brand, brand=> brand.menus)
@JoinColumn()
brand: Brand;
brand.entity.ts
@OneToMany((type) => Menu, menu => menu.brand)
menus: Menu;
When you attempt this, you cannot serve the API and you get the errors outlined in Failure Information.
This works COMPLETELY in Nest CLI.
WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts
WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/menu-item-modifiers/models/menu-item-modifier.entity.ts -> apps/fourburner-api/src/resources/menu-item-modifiers/models/menu-item-modifier.entity.ts
WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts
WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/orders/models/order.entity.ts -> apps/fourburner-api/src/resources/orders/models/order.entity.ts
WARNING in Circular dependency detected:
apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts -> apps/fourburner-api/src/resources/brands/models/brand.entity.ts -> apps/fourburner-api/src/resources/menus/models/menu.entity.ts -> apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts
followed by:
ReferenceError: Cannot access 'Brand' before initialization
at Module.Brand (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:1156:97)
at Module../apps/fourburner-api/src/resources/menus/models/menu.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/apps/fourburner-api/src/resources/menus/models/menu.entity.ts:49:10)
at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
at Module../apps/fourburner-api/src/resources/brands/models/brand.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:1161:83)
at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
at Module../apps/fourburner-api/src/resources/restaurants/models/restaurant.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:2664:85)
at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
at Module../apps/fourburner-api/src/resources/assignedroles/models/assigned_role.entity.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:979:95)
at __webpack_require__ (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/webpack:/webpack/bootstrap:19:1)
at Module../apps/fourburner-api/src/shared/auth/auth.service.ts (/Users/leeharris/Desktop/Repos/fourburner/dist/apps/fourburner-api/main.js:3864:110)
Please provide any relevant information about your setup:
NX Report output:
@nrwl/angular : 9.2.3
@nrwl/cli : 9.2.3
@nrwl/cypress : 9.2.3
@nrwl/eslint-plugin-nx : Not Found
@nrwl/express : Not Found
@nrwl/jest : 9.2.3
@nrwl/linter : 9.2.3
@nrwl/nest : 9.2.3
@nrwl/next : Not Found
@nrwl/node : 9.2.3
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 9.2.3
@nrwl/web : Not Found
@nrwl/workspace : 9.2.3
typescript : 3.8.3
3rd party versions:
"typeorm": "^0.2.24",
"pg": "^8.0.3",
Included above.
I've found this exact bug mentioned across several other products that use NX, but for some reason no bug report here. This is a common thing it seems for NX + NestJS users.
Could somebody please provide a minimal repo for us to debug?
Circular dependencies are difficult for build tools to handle. My _guess_ is that NestJS CLI may be circumventing this by using ts-node.Please provide a repro so that we can take a look.
Reproduction:
https://github.com/nbaua/nest-nx-typeorm-circular-dep
Found here:
https://github.com/typeorm/typeorm/issues/4526#issuecomment-570021560
Cannot access 'Brand' before initialization is another issue described here.
Solution is also provided there.
@FrozenPandaz is the reproduction good enough?
+1 I'm blocked from getting started with Nx because of this issue too. As described above the circular dependency works fine using the nest-cli way of building the project.
@rperfect Why are you blocked on this? It's just a warning. Everything still works fine as far as I can see.
Hmm - you're right it's just a warning. But I get dozens of the warnings and then the following error....
C:\Projects\decision-bear-nx\dist\apps\server\main.js:2152
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "AccountModel", function() { return AccountModel; });
^
ReferenceError: Cannot access 'AccountModel' before initialization
at Module.AccountModel (C:\Projects\decision-bear-nx\dist\apps\server\main.js:2152:104)
at Module../apps/server/src/app/domain/customer.model.ts (C:\Projects\decision-bear-nx\dist\apps\server\webpack:\apps\server\src\app\domain\customer.model.ts:25:14)
at __webpack_require__ (C:\Projects\decision-bear-nx\dist\apps\server\webpack:\webpack\bootstrap:19:1)
at Module../apps/server/src/app/domain/account.model.ts (C:\Projects\decision-bear-nx\dist\apps\server\main.js:2158:73)
at __webpack_require__ (C:\Projects\decision-bear-nx\dist\apps\server\webpack:\webpack\bootstrap:19:1)
at Module../apps/server/src/app/account/account.service.ts (C:\Projects\decision-bear-nx\dist\apps\server\main.js:327:79)
at __webpack_require__ (C:\Projects\decision-bear-nx\dist\apps\server\webpack:\webpack\bootstrap:19:1)
at Module../apps/server/src/app/account/account.module.ts (C:\Projects\decision-bear-nx\dist\apps\server\main.js:244:74)
at __webpack_require__ (C:\Projects\decision-bear-nx\dist\apps\server\webpack:\webpack\bootstrap:19:1)
at Module../apps/server/src/app/app.module.ts (C:\Projects\decision-bear-nx\dist\apps\server\main.js:599:81)
Ack - sorry @hakimio is right my error is actually #1667 I withdraw my comment about being blocked it seems to be working ok (but still with all the warnings) once I implement the solution in #1667. Thanks for giving me a push :-)
I experienced the same issue with the before initialization.
I tried:
https://github.com/nrwl/nx/issues/1667
But it break the global access to jest describe method :(
Any better solution?
Getting this with a Sequelize project as well, which is pretty much the same thing: User hasMany Items and an Item belongs to a User. User -> Item -> User.
I'm converting a project to nrwl/nx and the other developers are gonna get a little worried when they see all of those warnings.
It'd be nice if we could at least disable the circular checks for a particular lib (where I keep my entities/models)
I moved a NestJS project into an NX monorepo and I'm having the same issues. These warnings should at least be mute-able
@RafaelVidaurre you can. Just set showCircularDependencies to false in your @nrwl/node:build builder options in your angular.json file.
I disregarded using
repository.save()and uses the Query Builder as Insert function which everything works perfectly. As stated above as reference, confirming many of issues has been posted with exactly the same resolution of the issue. In development this issue would not occur to me, however after post-build(using webpack) running it will show Circular/Cylic Dependency.TypeORM is designed to use
@Decoratorto wire relation entities together, and it was the exactly the cause of it due to therepository.save()are loading up the Relational Entities during the insertion. The behavior of the Circular Dependencies is due to the ES Module is not supported yet, there are a lot of related blog post explaining it more detailed.
Reference and the resolution similar issue and it worked for me, #4526.
@JamesDelfini What's the actual resolution? Use strings instead of classes like here? That's a terrible "resolution".
@RafaelVidaurre you can. Just set
showCircularDependenciestofalsein your@nrwl/node:buildbuilder options in yourangular.jsonfile.
This breaks in the new 10.2.0 version of nx, as all circular dependencies are lint errors now.
@JamesDelfini What's the actual resolution? Use strings instead of classes like here? That's a terrible "resolution".
I forward that, losing typesafety in our entities because of a build tool quirk is indeed not a good solution.
As a workaround it works on my side, if you have any plugins detecting circular dependency remove it. When I have stumbled on this error, I added a plugin to detect the circular dependency and determine which file/code was causing it, then created a workaround of it. After that I just remove the plugin since as stated, it is a normal design architecture of TypeORM creating relations.
I'm having similar issues.
I added a library for typeorm, configured tsconfig.json and everything worked until I started adding relationships.
// ProfileEntity.ts
import { UserEntity } from '@loo/typeorm';
@Entity('profiles')
export class ProfileEntity {
...
@OneToOne('UserEntity', 'user')
user: UserEntity;
...
}
// UserEntity.ts
import { ProfileEntity } from '@loo/typeorm';
@Entity('users')
export class UserEntity {
...
@OneToOne('ProfileEntity', 'profile')
profile: ProfileEntity;
...
}
this allows the the app api to build and run with nx serve api which is a nestjs application, but throws the circular dependency warnings as below.

But, if I attempt to run any TypeORM cli commands, it fails.

In order to run typeorm cli commands, I need to replace the path aliases from the entities with the relative paths.
So instead of import { UserEntity } from '@loo-ah/typeorm' -> import { UserEntity } from './UserEntity'. At this point, nx serve api no longer works.
ReferenceError: Cannot access 'ProfileEntity' before initialization
However, leaving one entity importing from @loo-ah/typeorm and the other from the relative path, both nx serve and typeorm CLI works!!!
I also found another work around, instead of referencing from path aliases, if I import from ../../../../typeorm/src on both entities, both nx serve and typeorm CLI works, I still get the circular dependency warnings, but I'm not worry about those at this point.
I also have a @loo-ah/enums library which causes similar issues when importing into typeorm entities @loo-ah/enums.

again, the way to fix it was to import {...} from '../../../../enums/src'
I'm able to compile with these settings for tsconfig.app.json
"module": "CommonJS",
"target": "esnext"
@webberwang circular dependency warnings do not prevent the TS transpiler from finishing transpilation. ReferenceError: Cannot access 'Foo' before initialization is another bug.
Ok. After reading the following related issues:
https://github.com/nrwl/nx/issues/1667, https://github.com/typeorm/typeorm/issues/4526, https://github.com/typeorm/typeorm/issues/4190, and https://stackoverflow.com/questions/46589957/es6-modules-and-circular-dependency/46593566#46593566
I think I have a solution that doesn't involve using interfaces and strings for the @ManyToOne and @OneToMany decorators.
Meaning I believe to have figured it out such that you can use standard TypeORM conventions.
So this should work with my solution:
// menu.entity.ts
@ManyToOne((type) => Brand, brand=> brand.menus)
@JoinColumn()
brand: Brand;
//brand.entity.ts
@OneToMany((type) => Menu, menu => menu.brand)
menus: Menu[];
My solution might not work in all scenarios but for a specific scenario involving two files referencing each other, I believe this to work. (Meaning if there is a 3 or more file interlocking circular dependency wibbly-wobbly timey-wimey chain thing, then all bets are off because I didn't test that). So I'm using nx mono repo with NestJs and TypeORM. The nx builder is using webpack to bundle this stuff up. I left the tsconfig module setting to point to the default (esnext).
BOTTOM LINE: everything is the way you want it to be: everything is default for Nx and NestJs and everything is standard TypeOrm convention.
Make sure your entry point imports the @ManyToOne side first!
This produces the ReferenceError: Cannot access 'Brand' before initialization error:
// Main file somewhere
import { Brand } from '<somewhere>/brand.entity.ts'
import { Menu } from '<somewhere>/menu.entity.ts'
This does NOT produce the error:
// Main file somewhere
import { Menu } from '<somewhere>/menu.entity.ts'
import { Brand } from '<somewhere>/brand.entity.ts'
I suspect the reason for this is because the @OneToMany side of the relationship has an array of objects menus: Menu[]; rather than the type itself.
And so if I am correct, then in theory a bi-directional @OneToOne will still produce this issue. So stay tuned for when I encounter that problem. One idea that hasn't been tested yet is here: https://github.com/typeorm/typeorm/issues/4526#issuecomment-518744567 but I'm not sure if TypeORM relies on the design:type metadata to create objects or not.
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 馃檹
+1 just to keep this issue open
@Cammisuli FYI: duplicate issue https://github.com/nrwl/nx/issues/4358
Reproduction repo: https://github.com/sinnrrr/loonify-debug
I fixed that error message by adding compiler option => "module": "commonjs" in tsconfig.json which is under apps/yournestjsapp
but still getting these warnings and i want to get rid of these warnings...
Most helpful comment
+1 just to keep this issue open