nx: [Nest] Possible Bug in autogenerated TSConfig
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
- I’m reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)
Expected Behavior
In my API, I have a circular dependency between Account and Profile (one Account has one Profile assigned; one Profile belongs to one Account). This is realized with the TypeORM @OneToOne(...) relationship.
This api should properly build and run.
Current Behavior
The ng serve PROJECT command throws circular dependency warnings (see #1649 , #1661) and “refuses” to properly boot. In this context, i noticed the following error on the terminal:
ReferenceError: Cannot access 'Account' before initialization
at Module.Account (...\dist\apps\api\main.js:544:99)
...
If i trace down the stacktrace, i end up in the circular dependency between Account and Profile (as described above).
Solution
I manage to solve this issue by adding the following lines to the apps\api\tsconfig.app.json file:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs", // <-- here
"target": "es2017", // <-- here
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
The “autogenerated” tsconfig.app.json file when running
ng g @nrwl/nest:app api
omits those lines. In the sample project from nestjs ( https://github.com/nestjs/nest/blob/master/sample/01-cats-app/tsconfig.json ) these lines are required.
I think, we should add those lines by default - this can be done in the nest schematics right? Shall i add a PR for this @FrozenPandaz ?
When running ng serve api it still throws a lot of circular dependency warnings, however, the application successfully “compiles” and can be executed. The issue with the circular dependency was introduced somewhere between 8.2 and 8.4, i guess…
All the best
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 14
- Comments: 17 (7 by maintainers)
@fstn I can run jest test without any problem. Here are my tsconfigs for reference:
apps/api/tsconfig.spec.jsonapps/api/tsconfig.jsonapps/api/tsconfig.app.jsonThe problem must be on your side.
@bcabanes and @vsavkin : I would advise to re-open this issue because the problem is actually not solved!
Dear @vsavkin , @FrozenPandaz and @bcabanes
i recently stumbled upon the same issue again, as i started to work on another
nrwl/nestproject. I vaguely remembered that there was an issue in thetsconfigfile somewhere when using the autogenerated stubs. Can you please fix this?All the best, Johannes
Just stumbled upon this issue on new NX project. @vsavkin , @FrozenPandaz and @bcabanes
This issue was closed thinking issue is with Dependency Injector and bad architecture. This is not true, this issue is about simple
import .. from ..statements.Imagine using Typeorm, which NestJS natively support and defining bi-directional relation between two tables (two classes). A imports B, while B imports A. Just as in example here - https://typeorm.io/#/many-to-one-one-to-many-relations User imports Post and Post imports User.
Bare Nestjs works out of the box, while created from NX does not.
NX should use same module and target config as NestJs uses. Please, fix the issue
Thank you
Dear @bcabanes , i don’t think, that the solution provided by me is a “workaround” but rather the proper solution! NestJs requires these 2 properties in the
tsconfig.jsonfile - andnrwl/nxis missing those.I repeat, this is not an architecture problem with my application i am building - you can observe this behaviour with a freshly built nestjs application in
nrwl/nx. Shall i provide a repository for you to reproduce the issue? I can provide you with one in the following week!I can confirm the issue itself and the solution too. Setting options to commonjs/es2017 will still throw warnings but compile successfully.