Repository navigation
Tricky issue with Require() #23144
Description
Activity
Can you provide a simple, complete example that reproduces the issue?
Are you just seeing the
require()cache?Without a concrete example it's hard to say, but it sounds like you have a cyclic module dependency: https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_cycles
When
main.jsloadsa.js, thena.jsin turn loadsb.js. At that point,b.jstries to loada.js. In order to prevent an infinite loop, an unfinished copy of thea.jsexports object is returned to theb.jsmodule.b.jsthen finishes loading, and itsexportsobject is provided to thea.jsmodule.Thank all for your prompt responses. I believe @richardlau may have hit it in the head: I have circular
require()calls in the affected modules.Because I'm a n00b and this is the first time I've run into this issue stemming from poor planning on my part, I'd like to as for some input on this: what's the best way to address this?
I know that this is not the best forum for this but at this time I have your attention :)
Here are two possibilities:
A
Move around the require calls. Some, but certainly only a few, such problem come from the order of imports.
B
Have one of the module lazy-load it's dependencies.
That is, actually require them the first time some exported function is run.IMHO, the first step is to think carefully about the high level design of the program, specifically in the context of the dependency inversion principle. If you really need both modules to know about each other on some level, consider setting up a module that does the orchestration between both of them, for example. You may be able to change to this model relatively easily and reap its benefits, including better dependency management.
Again, thank you all for your input. This is invaluable to me.
Sounds like this has been addressed now. I'm going to close it out as I'm trying to keep the issue tracker tidy but please feel free to reopen.
Hello all,
I'm noticing a tricky issue that may be hard to explain. At a high level, it seems as though
require()struggles with recently moved/renamed files.I'm seeing this issue when trying to require some functions from files within the same folder. I have three files, two with identical contents (
ctrl-a, ctrl-c, ctrl-vbetween files) in the same folder:a.js,copy-of-a.js, andb.js. I require functions from these files elsewhere with the following code:const {someFunction, someOtherFunction, etc} = require('../../middlewares/a.js')Both
a.jsandcopy-of-a.jshave functions required inb.jsand vice versa:b.jshas some functions required ina.jsandcopy-of-a.js. All paths are correct, as no errors are seen for missing files.Here's where things get weird. Calling a function in
b.jsthat itself uses a requires function froma.jswill yield an error statingTypeError: someFunction is not a functionalong with the stack trace of where the error occurred. Updating the code to require the same function fromcopy-of-a.jsinstead works just fine.I'm not sure that I have can do to provide more information about this behavior. All I can think of is that I had recently run the following command to rename
a.jsfrom something else:mv someOldName.js a.jsEven this is theory is thin. Refactoring the
require()from other files to point to the new name works just fine. In my eyes the only difference between the twoafiles is that one is brand new and the other one has been around for a while.Any suggestions on how to provide more information to properly see where the issue may lay?
Thanks!