79118855

Date: 2024-10-23 16:38:21
Score: 0.5
Natty:
Report link

Imports referencing index.ts in same directory

I had a similar issue to Timothy Matthews' answer here.

• Uncaught ReferenceError: Cannot access main. js: 426358
'Child1Component' before initialization at ‹static initializer> (main.is:426358:225)

Browser console error stack trace screenshot

Cause

Example file structure:

/parent.component.ts
/child1/child1.component
/child2/child2.component
/index.ts (exports all the above)

My IDE (intellij) set the imports in parent.component.ts to point at the index.ts in the same directory which also contains the child components, which causes the issue:

import {
  ChildComponent1,
  ChildComponent2,
} from '../';

Fix

I updated my imports to this, which fixed the issue:

import { ChildComponent1 } from '../child1/child1.component';
import { ChildComponent2 } from '../child2/child2.component';

Timothy Matthews notes above that this would also work, but I cannot confirm:

Change index.ts to _index.ts

import {
  ChildComponent1,
  ChildComponent2,
} from '../_index';
Reasons:
  • Blacklisted phrase (0.5): I cannot
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Nick Johnson