Sekrab Garage

ReferenceError : Cannot access 'X' before initialization

The "before initialization error" of Angular, one more reason behind it

AngularTip November 7, 21

Searching the net for the "ReferenceError : Cannot access '*' before initialization" usually digs up a solution in tsconfig or fixing the A injected in B, B injected in C, C injected in A narrative. In addition to forward-refs, and @injector related solutions. But my problem was fixed with simply getting rid of the index.ts, that exports all from sub files, in a module.

The situation was as follows: using a directive globally, on the root of the application, that injected a service from a module, imported from an index.ts file of the module. That kept failing with "Cannot access directive before initialization." After trial and error, I let go of the index, and imported services from their location. And it worked.