Accessing a circular dependency early with re-export, an error occurs
See original GitHub issueBug report
What is the current behavior?
This is a simplified example:
// src/barrel/a.js
export function a() {}
// src/barrel/b.js
import {a} from "./index";
console.log(a); // expected undefined
export function b() {}
// src/barrel/index.js
export * from "./b";
export * from "./a";
// src/index.js (entry point)
import {b} from "./barrel";
b();
When I run this example, I get an Cannot read property 'a' of undefined
error in console.log(a);
line of b.js
.
What is the expected behavior?
I expect undefined to be printed to log without error.
I know that if I change the export order of index.js
or import a.js
directly from b.js
it works fine.
But I expect modules with unresolved circular imports to evaluate to undefined.
If I run the same code with Webpack 4(4.44.2
), it prints undefined without error.
Other relevant information: webpack version: 5.10.1 Node.js version: 14.15.1 Operating System: Ubuntu 20.04.1 LTS (WSL) Additional tools: None
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How to resolve circular dependencies in typescript
I could resolve some circular dependencies by removing them from the modules index. ts files and directly importing them with a fully-qualified ...
Read more >How to Eliminate Circular Dependencies from Your JavaScript ...
Circular dependencies are usually an indication of bad code design, and they should be refactored and removed if at all possible.
Read more >So you got a circular import in Python. | by Hadrien Hamana
People claim that the problem is aggravated by using from module import Type instead of only import module and then using module.Type ....
Read more >Don't make the same mistake I did. Avoid circular ... - Reddit
Assuming that because my code built and worked, that the circular dependencies were not going to be a problem.
Read more >Export and Import - The Modern JavaScript Tutorial
At first sight, “import everything” seems such a cool thing, ... A common mistake when starting to use modules is to forget curly...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Maybe better to stick on webpack@4 in this case
I think it is very hard/impossible to fix without breaking something other
I meet the same issue like @HealGaren said when update to webpack@5.68 from webapck@4.46, Is this problem fixed?There was a risk to refactoring the legacy code.