Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Madge cannot find dependency if required via for loop #115

Closed
dustyhorizon opened this issue Mar 17, 2017 · 4 comments
Closed

Madge cannot find dependency if required via for loop #115

dustyhorizon opened this issue Mar 17, 2017 · 4 comments

Comments

@dustyhorizon
Copy link

Hello,

I have a code where I read from file and require from a for loop. This does not display any dependant module in the resultant graph.

E.g.

let files = require('configs')
for (let file of files) {
    require(file)
}

Any possible fix?

@mrjoelkemp
Copy link
Contributor

Thanks for reporting! Seems like a runtime analysis is necessary to determine the modules being required, which is beyond the reach of what dependency-tree (and static analysis) can do.

I don't think we can do much here without building fragile and specific sniffs to fake runtime execution, but curious to hear thoughts from others.

@pahen
Copy link
Owner

pahen commented Mar 17, 2017

I agree with @mrjoelkemp. I don't see any way to fix this without a introducing a lot of complexity and possible bugs. So I'm closing this for now. But if anyone has a brilliant solution to this I'm very happy to discuss it :)

@pahen pahen closed this as completed Mar 17, 2017
@bagrounds
Copy link

@dustyhorizon what is your use case for this?

Typically when we require something, we want to assign it to a variable so we can use it.

In the example code you provide, each item required must have some side effect (like introducing or modifying global variables) to do anything useful, right?

So I'm not sure what you're trying to accomplish, but maybe the following will help.

If 'configs' is something like this:

module.exports = ['config1', 'config2', ...]

Maybe you could refactor it to something like this:

module.exports = [require('config1'), require('config2'), ...]

Then you can just

let files = require('configs')

And you should get a nice dependency graph.

@dustyhorizon
Copy link
Author

I have a list of 'modules' (in either enabled or disabled state) in a 'config' file (exported as an object) in which the loader will iterate and require each item under the enabled key. My first project in nodejs so not very good at optimizing and refactoring it yet haha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants