FAQ
Let us say I have installed a standard npm module, 'foo' (any resemblance
to real modules is unintended and purely coincidental). foo, in turn,
requires bar. After running 'npm install foo;, my root directory will look
like:

app.js
node_modules/
..foo/
..package.json
....node_modules/
......bar/

(irrelevant details left out).

From my root directory, I can do require('foo') and load foo.

What if I want access to 'bar'? I *cannot* do require('bar') because it is
not in my node_modules path.

How do I access the 'bar' package from my app?


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

  • Deitch at Jun 28, 2012 at 1:09 pm
    To be more specific, I don't want access to any instance of 'bar', e.g. if
    it appears elsewhere. I want the actual cached instance used by 'foo'.
    On Thursday, June 28, 2012 9:05:00 AM UTC-4, deitch wrote:

    Let us say I have installed a standard npm module, 'foo' (any resemblance
    to real modules is unintended and purely coincidental). foo, in turn,
    requires bar. After running 'npm install foo;, my root directory will look
    like:

    app.js
    node_modules/
    ..foo/
    ..package.json
    ....node_modules/
    ......bar/

    (irrelevant details left out).

    From my root directory, I can do require('foo') and load foo.

    What if I want access to 'bar'? I *cannot* do require('bar') because it is
    not in my node_modules path.

    How do I access the 'bar' package from my app?
    --
    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
  • Oliver Leics at Jun 28, 2012 at 3:04 pm
    https://gist.github.com/3011868
    On Thu, Jun 28, 2012 at 3:09 PM, deitch wrote:
    To be more specific, I don't want access to any instance of 'bar', e.g. if
    it appears elsewhere. I want the actual cached instance used by 'foo'.

    On Thursday, June 28, 2012 9:05:00 AM UTC-4, deitch wrote:

    Let us say I have installed a standard npm module, 'foo' (any resemblance
    to real modules is unintended and purely coincidental). foo, in turn,
    requires bar. After running 'npm install foo;, my root directory will look
    like:

    app.js
    node_modules/
    ..foo/
    ..package.json
    ....node_modules/
    ......bar/

    (irrelevant details left out).

    From my root directory, I can do require('foo') and load foo.

    What if I want access to 'bar'? I *cannot* do require('bar') because it is
    not in my node_modules path.

    How do I access the 'bar' package from my app?
    --
    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


    --
    Oliver Leics @ G+
    https://plus.google.com/112912441146721682527

    --
    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
  • Avi Deitcher at Jun 28, 2012 at 3:07 pm
    Awesome, thank you.
    On Jun 28, 2012, at 11:04 AM, Oliver Leics wrote:

    https://gist.github.com/3011868
    On Thu, Jun 28, 2012 at 3:09 PM, deitch wrote:
    To be more specific, I don't want access to any instance of 'bar', e.g. if
    it appears elsewhere. I want the actual cached instance used by 'foo'.

    On Thursday, June 28, 2012 9:05:00 AM UTC-4, deitch wrote:

    Let us say I have installed a standard npm module, 'foo' (any resemblance
    to real modules is unintended and purely coincidental). foo, in turn,
    requires bar. After running 'npm install foo;, my root directory will look
    like:

    app.js
    node_modules/
    ..foo/
    ..package.json
    ....node_modules/
    ......bar/

    (irrelevant details left out).

    From my root directory, I can do require('foo') and load foo.

    What if I want access to 'bar'? I *cannot* do require('bar') because it is
    not in my node_modules path.

    How do I access the 'bar' package from my app?
    --
    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


    --
    Oliver Leics @ G+
    https://plus.google.com/112912441146721682527

    --
    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
  • Deitch at Jun 28, 2012 at 4:36 pm
    Strangely, it doesn't work. require.resolve(module,base) does not do it. It
    just ignores base entirely?
    On Thursday, June 28, 2012 11:04:27 AM UTC-4, Oliver Leics wrote:

    https://gist.github.com/3011868
    To be more specific, I don't want access to any instance of 'bar', e.g. if
    it appears elsewhere. I want the actual cached instance used by 'foo'.

    On Thursday, June 28, 2012 9:05:00 AM UTC-4, deitch wrote:

    Let us say I have installed a standard npm module, 'foo' (any
    resemblance
    to real modules is unintended and purely coincidental). foo, in turn,
    requires bar. After running 'npm install foo;, my root directory will
    look
    like:

    app.js
    node_modules/
    ..foo/
    ..package.json
    ....node_modules/
    ......bar/

    (irrelevant details left out).

    From my root directory, I can do require('foo') and load foo.

    What if I want access to 'bar'? I *cannot* do require('bar') because it
    is
    not in my node_modules path.

    How do I access the 'bar' package from my app?
    --
    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


    --
    Oliver Leics @ G+
    https://plus.google.com/112912441146721682527
    --
    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
  • Deitch at Jun 28, 2012 at 11:36 pm
    It doesn't work, because require.resolve() only takes one argument, doesn't
    accept a relative path.
    On Thursday, June 28, 2012 12:36:27 PM UTC-4, deitch wrote:

    Strangely, it doesn't work. require.resolve(module,base) does not do it.
    It just ignores base entirely?
    On Thursday, June 28, 2012 11:04:27 AM UTC-4, Oliver Leics wrote:

    https://gist.github.com/3011868
    To be more specific, I don't want access to any instance of 'bar', e.g. if
    it appears elsewhere. I want the actual cached instance used by 'foo'.

    On Thursday, June 28, 2012 9:05:00 AM UTC-4, deitch wrote:

    Let us say I have installed a standard npm module, 'foo' (any
    resemblance
    to real modules is unintended and purely coincidental). foo, in turn,
    requires bar. After running 'npm install foo;, my root directory will
    look
    like:

    app.js
    node_modules/
    ..foo/
    ..package.json
    ....node_modules/
    ......bar/

    (irrelevant details left out).

    From my root directory, I can do require('foo') and load foo.

    What if I want access to 'bar'? I *cannot* do require('bar') because
    it is
    not in my node_modules path.

    How do I access the 'bar' package from my app?
    --
    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


    --
    Oliver Leics @ G+
    https://plus.google.com/112912441146721682527
    --
    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
  • Deitch at Jun 28, 2012 at 11:45 pm
    OK, much simpler answer, believe it or not:

    var foo = require('./foo')
    , barOfFoo = require.cache[require.resolve('./foo')].require('./bar');

    --
    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
  • Oliver Leics at Jun 29, 2012 at 12:26 pm

    Just to let the rest of the ML see your response:
    On Fri, Jun 29, 2012 at 12:09 PM, Oliver Leics wrote:
    On Fri, Jun 29, 2012 at 1:36 AM, deitch wrote:
    It doesn't work, because require.resolve() only takes one argument,
    doesn't
    accept a relative path.
    Works with node v0.7.11, but maybe not with lower node-versions.
    On Fri, Jun 29, 2012 at 1:56 PM, Avi Deitcher wrote:
    Aha! That might be it. I am working in this case with 0.6.x
    - o

    --
    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
  • Thomas Blobaum at Jun 29, 2012 at 3:25 am
    What's wrong with require('foo/node_modules/bar')

    Or,
    npm install bar
    npm install foo
    require('bar')

    --
    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
  • Oliver Leics at Jun 29, 2012 at 9:22 am

    On Fri, Jun 29, 2012 at 5:25 AM, Thomas Blobaum wrote:
    What's wrong with require('foo/node_modules/bar')
    'bar' might not be loaded from the node_modules-directory of 'foo'.
    Or,
    npm install bar
    npm install foo
    require('bar')
    Nothing wrong with that. Do it this way!



    The OP asks for a way to access the exports of a module as it was
    loaded by another module. This is not recommended. Avoid this by all
    means. Only do this if you really really have to.

    --
    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
  • Deitch at Jun 29, 2012 at 11:55 am
    Correct on the first. But you cannot do

    npm install bar
    npm install foo

    This will only work if both bar and foo are in the *current* node_modules.
    But if bar is only inside of foo, it will not find it.
    On Friday, June 29, 2012 5:22:01 AM UTC-4, Oliver Leics wrote:
    On Fri, Jun 29, 2012 at 5:25 AM, Thomas Blobaum wrote:
    What's wrong with require('foo/node_modules/bar')
    'bar' might not be loaded from the node_modules-directory of 'foo'.
    Or,
    npm install bar
    npm install foo
    require('bar')
    Nothing wrong with that. Do it this way!



    The OP asks for a way to access the exports of a module as it was
    loaded by another module. This is not recommended. Avoid this by all
    means. Only do this if you really really have to.
    --
    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
  • Deitch at Jun 29, 2012 at 11:55 am

    The OP asks for a way to access the exports of a module as it was
    loaded by another module. This is not recommended. Avoid this by all
    means. Only do this if you really really have to.
    I agree 100%. It would not use this in production. This is only for
    temporary patching of a deficient module inside a testing suite.

    --
    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
postedJun 28, '12 at 1:05p
activeJun 29, '12 at 12:26p
posts12
users3
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase