If you mess with the require() cache, it's very possible to create a
situation where you cause infinite loops by require() cycles
re-reading the same files repeatedly and synchronously, and these are
difficult to debug.
It's much better to just export a function, and have your consumer
call that f
Looks you want separate behaviour for every module require()ing you. Why
not force every caller to instantiate an object of your custom class type,
passing in a directory location? This has the added side effect of allowing
a single module to instantiate multiple objects with potentially differe
Depends, if a module holds a reference to the module being reloaded and
there is a shared cache being used by that module. You would still end up
with 2 references to different things. I would need to know way more about
whats trying to be done / API to make any sane comment on if it looks like
Definitely you would need to develop your module with the understanding
that it will be loaded more then once, but it should not effect 3rd party
libraries as they would still be cached correct?
Its the only approach that I can see to solve the users scenario, another
possible solution would be
If the module uses some form of internal storage (for caching, module level
object registration, etc.) it would be duplicated and then you could see
some interesting cache mismatch bugs.
For example in the following module 2 registries would exist rather than
the expected one if completely relo
Other then the obvious speed implications, there shouldn't be any other
downsides to removing the cache? Although not the best case scenario, I
believe its the only way to know the file that is requiring the module, as
module.parent.filename will update to be the latest file.
On Sunday, Novem
You can look at module.parent.filename, but that's just the first
module to require() you, not necessarily the most recent, due to
caching.
Deleting yourself out of the cache is likely pretty stupid. Don't do that.
But what you're probably more interested in is require.main.filename
or process.c
You'll have to disable caching and it will work, this saved would print the
directory that required the module;
delete require.cache[__filename];
module.exports = function() {
var path = require('path');
console.log(path.dirname(module.parent.filename));
};
On Saturday, Novemb
On Mon, Nov 12, 2012 at 3:22 AM, boden wrote:
> doesn't *have* to, but does for ease of use -- it defaults some paths
> relative to the caller's (the module require()ing) module location...
> obviously there are other approaches such as forcing the require()er to
> pass in those paths rather than
doesn't *have* to, but does for ease of use -- it defaults some paths
relative to the caller's (the module require()ing) module location...
obviously there are other approaches such as forcing the require()er to
pass in those paths rather than defaulting them.
i'm not all that seasoned in the n
guess there is no other way. but the whole task smells like hell to me. why
does your module need to know who required it?
Am Samstag, 10. November 2012 15:02:50 UTC+1 schrieb boden:
>
> hi all,
> trying to find the proper way to determine the path of the module
> requiring my module given the f
11 matches
Mail list logo