-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot access before initialization error in NestJs #6498
Comments
I am having the same issue without using Nestjs but I do have a decorator on the constructor of my class. I will try to make a reproducible case. For me it also improves things when switching back to targetting |
For anyone having the problem with |
I am getting the same error after upgrading swc. I am using ts-node with swc, via my tsconfig.json
This error is happening pretty much everywhere there is a typescript reference to a class type. For example, in my entity class, the fields will reference typescript types of other entity classes. It seems to be a problem with the loader not understanding that these are merely type references and not instances of the class object. I can remove the error by the following, but I have to do it all throughout my code base! Create a trivial typescript type
Then change all my class type references from
then it becomes
When I do this, the circular dependency is resolved. It seems to force the class reference to be treated as just a type, rather than an actual class object instance. Very strange, |
Maybe this is just #5047 . I sure would like to know what magic makes |
@dustin-rcg do you have decorators on that classes? With decorators and I also have the same issues in GraphQL models and looking how to resolve it. |
I've created a related issue in NestJS Graphql Repo nestjs/graphql#2568 |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
1 similar comment
This comment was marked as spam.
This comment was marked as spam.
This is definitely due to this, I think SWC should understand that the import is used as a type and not a value. I don't know if that's possible. |
I just ran into this as well, while attempting to migrate a NestJS app from The issue is a heart caused by a circular dependency, but its also how This is the relevant parts from the output of // require statements are here
let TeamTasksService = TeamTasksService_1 = class TeamTasksService { ... }
// rest of code
exports.default = TeamTasksService; The same file transformed with Object.defineProperty(exports, "default", {
enumerable: true,
get: ()=>TeamTasksService
});
// require statements are here
let foo = require('./foo') // which references this file
// rest of code The error is surfaced because this file is part of a circular dependency so the |
Is it really being imported as type ? If so,
With an eslint rule this can be fixed automatically. Note:
ref: https://typescript-eslint.io/rules/consistent-type-imports/#usage-with-emitdecoratormetadata If the above is true, I don't think this is an easy fix. |
@magic-akari I did that but still got the same error due to how the generated js file looks like. I'm not using with jest but |
This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think this was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
I'm using swc in a NestJs project and i'm facing the following issue when i run my unit test with jest:
I think the error is caused by the automapper library which i use in the
document-data.entity.ts
:Input code
Config
FYI, it works with the swc config
"target": "es5"
but then i've got code coverage issuesPlayground link
No response
Expected behavior
I'm expecting the test to run correctly
Actual behavior
No response
Version
1.13.19
Additional context
No response
The text was updated successfully, but these errors were encountered: