Angular ERROR TypeError: Cannot read properties of undefined (reading 'onDestroy')
Asked Answered
E

12

33

I have problem. Angular is sometimes showing error ERROR TypeError: Cannot read properties of undefined (reading 'onDestroy')

I am getting this error when I am running ng serve, but if I use angular universal, then page is sometimes loading properly and sometimes it is showing same error. I found out, that this error started when I started making modularization of my app.

Angular version 13.3

Equilibrant answered 16/4, 2022 at 18:41 Comment(1)
Same error on Angular 12Fecal
S
100

I had the same issue. Try to remove the .angular directory! worked for me.

Semolina answered 25/4, 2022 at 12:28 Comment(6)
Excatly, delete .angular and re-run ng s. Thanks a lot!Fantast
Mind blowing, thanks @Faisal Alashir. This solution worked for me too... But what's the reason behind this? Why did it throw an error at first place?Hanhhank
but what is the reason behind .angular directoryMission
Should be marked as resolve answerOwlish
I guess the reason behind this is something to do with compiled code that is no longer there, that's why it happens. Deleting the .angular is more like deleting the cache which makes the next ng serve fresh.Meaghan
Thank you... Unbelievable that this worked! ;)Checkrein
D
5

This happened when I was trying to use components in an ngSwitchCase that rendered ng-templates with components in them.

The components that did not implement ngOnDestroy would give me this error.

short answer, try adding ngOndestroy to components that might be rendering on the fly?

Delanie answered 20/4, 2022 at 22:57 Comment(2)
This is the correct answer. This error happens only in prod mode, and you need to add the ngOnDestroy to component especially when u use structural directives over them. deleting the angular dir as well seems helpingLakin
This error does not happen only on prod mode, so.. unfortunately this answer didn't help.Fecal
E
4

I found a solution for this issue; it is hard and not typical bug. It happens if you don't finish modularizing your code, where part of code you are using as modules and part as component imports.

Components that aren't modularized and used in app.component.html different than

<router-outlet></router-outlet>

are causing this error.

In my case, it was:

<app-navbar></app-navbar>

What is the issue?

If you have this error, and you add to navbar f.e. <div></div> and then remove it app works normally.

What is the solution?

You have to modularize all components used in app.component.html properly.

Equilibrant answered 16/4, 2022 at 18:41 Comment(1)
this saved my bum. I mean... literally changing the .html file with anything (like the div you proposed) fixes it. so, so silly! many thanks @EquilibrantWeka
P
1

I had the same problem. in my case because I defined my model without any default values and bind it to HTML like this myModel.value. so because the myModel.value is not value, I got that error. I replaced it with myModel?.value and the error fixed

Pepsinate answered 22/4, 2022 at 9:47 Comment(0)
B
1

We had the same issue and it was caused by using "npm install" with parameter "force" in the dev environment, but the build server didn't use force. This was causing the prod build to use old packages which caused the error.

Betulaceous answered 5/4, 2023 at 6:37 Comment(0)
L
1

If non of fixes above helped, the same error appears when you forget to include CommonModule in import section of your @NgModule. Be aware and hope it helps somebody.

Laney answered 13/6, 2023 at 13:0 Comment(0)
E
0

I had the same problem, which was caused by a faulty import of a module. The typescript alias contained a double slash. Similar to lib//module, and once we removed this, the app worked again.

Expression answered 26/7, 2022 at 14:11 Comment(0)
S
0

I solved my issue with updating my A13 to A14.

Suppletion answered 6/8, 2022 at 10:19 Comment(0)
S
0

We had this issue on production. Turns out the issue was in our git repository. Due to some squash commits to the master branch, it seems to have gotten messed up somehow.

Fixed it by enabling the "Clean" option with clean option "Sources" in Azure DevOps pipeline, under "get sources" at the top (Classic pipeline).

Surrounding answered 15/11, 2022 at 14:51 Comment(0)
A
0

If none of above is working for you, try updating your packages via npm install. It worked for me :)

Amish answered 20/6, 2023 at 12:41 Comment(1)
Please improve formatting, quality and add some more information.Thomajan
Q
0

In my case I had neglected to add the import for NgFor in my @Component({imports:[NgFor]}) array.

Hope this helps someone.

Quintin answered 22/12, 2023 at 7:47 Comment(0)
D
0

First remove node modules & also remove package-lock.json. Then install node modules again. npm i-f It's work for me

Dhammapada answered 29/7 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.