question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing a circular dependency early with re-export, an error occurs

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Dec 15, 2020

Maybe better to stick on webpack@4 in this case

Even if the behavior is not supported, I think it is possible to implement a change to safely evaluate to undefined without throwing an error. (If necessary, I will try to send a PR.)

I think it is very hard/impossible to fix without breaking something other

0reactions
genshinwcommented, Feb 19, 2022

In the latest release (i.e. 5.26.2), it is undefined, fixed 😄

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. image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found