trying to find the proper way to determine the path of the module require()ing mine given the following requirements:
I ended up getting this to work by patching Module._load() and capturing the callers path in a closure as shown here: github
However this doesn't feel right -- I must be missing something.
Patching module._load
is the best way I think. You can only get the first module who called your module by module.parent.filename
. And the whole children
and parent
properties of module are only true for the first time load. According to the source code, if it's cached you can't rely on anything. You can try remove yourself from the cache(delete require.cache[__filename]
;) which then everytime your module is required, you get the right parent, but that will increase your load time.