FAQ
hi all,
trying to find the proper way to determine the path of the module requiring
my module given the following requirements:

* must be strict mode complaint
* cannot depend on node arguments (i.e. process.argv)
* must work when being required from different modules in the same
execution (i.e. my module is already cached)

i ended up patching Module._load() to capture the request and then wrapping
it in a closure like shown
here: https://github.com/bodenr/expose/blob/master/index.js

however this doesn't feel right -- i must be missing something.

thx in advanced

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Greelgorke at Nov 12, 2012 at 8:46 am
    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 following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Boden at Nov 12, 2012 at 11:22 am
    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 node space, but i haven't gotten a whiff
    of hell yet... anything in particular that drums up the smell of hell for
    you that I should be aware of?
    On Monday, November 12, 2012 3:46:29 AM UTC-5, greelgorke wrote:

    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 following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Nov 17, 2012 at 5:03 pm

    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 defaulting them.

    i'm not all that seasoned in the node space, but i haven't gotten a whiff
    of hell yet... anything in particular that drums up the smell of hell for
    you that I should be aware of?

    If you're making assumptions about your caller, you're likely asking for
    trouble, now or in the future. For an example, see:

    https://github.com/joyent/node/issues/4233#issuecomment-10046241

    --
    Martin Cooper

    On Monday, November 12, 2012 3:46:29 AM UTC-5, greelgorke wrote:

    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 following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here: https://github.com/**
    bodenr/expose/blob/master/**index.js<https://github.com/bodenr/expose/blob/master/index.js>

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines:
    https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Jason Brumwell at Nov 18, 2012 at 2:34 am
    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, November 10, 2012 9:02:50 AM UTC-5, boden wrote:

    hi all,
    trying to find the proper way to determine the path of the module
    requiring my module given the following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Isaac Schlueter at Nov 19, 2012 at 2:49 am
    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.cwd() anyway.


    On Sun, Nov 18, 2012 at 11:34 AM, Jason Brumwell
    wrote:
    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, November 10, 2012 9:02:50 AM UTC-5, boden wrote:

    hi all,
    trying to find the proper way to determine the path of the module
    requiring my module given the following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines:
    https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Jason Brumwell at Nov 19, 2012 at 1:44 pm
    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, November 18, 2012 9:49:18 PM UTC-5, Isaac Schlueter wrote:

    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.cwd() anyway.


    On Sun, Nov 18, 2012 at 11:34 AM, Jason Brumwell
    <jason.b...@gmail.com <javascript:>> wrote:
    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, November 10, 2012 9:02:50 AM UTC-5, boden wrote:

    hi all,
    trying to find the proper way to determine the path of the module
    requiring my module given the following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines:
    https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nod...@googlegroups.com<javascript:>
    To unsubscribe from this group, send email to
    nodejs+un...@googlegroups.com <javascript:>
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Bradley Meck at Nov 19, 2012 at 2:43 pm
    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 reloaded:

    ```javascript
    var registeredHandlers = {};
    exports.register = function (event, fn) {registeredHandlers[event] = fn;}
    exports.fire = function (event) {registeredHandlers[event]();}
    ```

    Soo... if you setup a "prestart" function, then it gets reloaded, and
    someone attempts to fire "prestart" on the reloaded module; it would not
    contain the function originally registered as "prestart" and be a cache
    mismatch, which would be very hard to debug.

    Don't introduce side effects into other authors' modules, so never ever do
    this without knowing you can cause cache mismatches.

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Jason Brumwell at Nov 20, 2012 at 12:58 am
    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 to utilize something like
    http://www.npmjs.org/package/traceback if your collecting the information
    via a function call.

    Other then that the only other way I can see is to edit module.js and
    inject the actual parent into the module. Seems kind of an odd api for the
    module when the parent isn't actually the current parent for the module. I
    can see where knowing the actual parent would be very useful, if it were
    possible to inject the current parent into the cache to make it retrievable
    could be beneficial.
    On Monday, November 19, 2012 9:43:42 AM UTC-5, Bradley Meck wrote:

    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 reloaded:

    ```javascript
    var registeredHandlers = {};
    exports.register = function (event, fn) {registeredHandlers[event] = fn;}
    exports.fire = function (event) {registeredHandlers[event]();}
    ```

    Soo... if you setup a "prestart" function, then it gets reloaded, and
    someone attempts to fire "prestart" on the reloaded module; it would not
    contain the function originally registered as "prestart" and be a cache
    mismatch, which would be very hard to debug.

    Don't introduce side effects into other authors' modules, so never ever do
    this without knowing you can cause cache mismatches.
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Bradley Meck at Nov 20, 2012 at 4:33 pm
    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
    there is a reference leak and shared cache situation.

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Dhruvbird at Nov 21, 2012 at 1:50 pm
    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 different
    arguments (something you might need later - it at least smells like it).
    On Saturday, November 10, 2012 9:02:50 AM UTC-5, boden wrote:

    hi all,
    trying to find the proper way to determine the path of the module
    requiring my module given the following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Isaac Schlueter at Nov 21, 2012 at 11:01 pm
    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 function, rather than re-run your module on every load.

    module.exports = function(arg) {
    // do stuff
    }

    // the consumer
    var blarg = require('blahrg');
    var myBlarg = blarg(__filename);

    On Wed, Nov 21, 2012 at 10:50 PM, dhruvbird wrote:
    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 different
    arguments (something you might need later - it at least smells like it).

    On Saturday, November 10, 2012 9:02:50 AM UTC-5, boden wrote:

    hi all,
    trying to find the proper way to determine the path of the module
    requiring my module given the following requirements:

    * must be strict mode complaint
    * cannot depend on node arguments (i.e. process.argv)
    * must work when being required from different modules in the same
    execution (i.e. my module is already cached)

    i ended up patching Module._load() to capture the request and then
    wrapping it in a closure like shown here:
    https://github.com/bodenr/expose/blob/master/index.js

    however this doesn't feel right -- i must be missing something.

    thx in advanced
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines:
    https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedNov 10, '12 at 4:42p
activeNov 21, '12 at 11:01p
posts12
users7
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase