In the interest of bringing up all options, one practice I like is to:
1. Place all internal dependencies within a top-level `lib` directory.
2. Place an index in the `lib` directory.
3. Make the `lib/index.js` useful in one or both of two ways.
(Dependent on project needs)
The first way is the "normal" require-everything-and-reexport. So if
you have two modules under `lib`, "a" and "b", your index looks like
so:
module.exports = {
a: require('./a'),
b: require('./b')
}
and is used thusly:
var lib = require('./some/path/lib')
lib.a.doSomething()
The second way is the "sexy" way.
module.exports = function lib(name) {
return require('./' + name)
}
to be used this way:
var lib = require('./some/path/lib')
, a = lib('a')
a.doSomething()
In both cases, you don't lose the most important information (_where
are these modules located?_), while still cleaning up the code a bit
and, also importantly, _reducing the cost of creating new modules_.
Enjoy,
Schoon
On Sat, Feb 9, 2013 at 8:50 AM, Isaac Schlueter <[email protected]> wrote:
> Geerten,
>
> Keep in mind, doing things in this way is going away from the typical way
> that node programs are written. There's nothing wrong with it from a
> technical point of view. However, you may be inviting unnecessary confusion
> for those who read your code later, if they are familiar with traditional
> node module practices. When the see require("foo"), they're going to think
> "external dependency", and go looking to package.json and npm for the code.
>
> I would recommend for that reason that you write small independent modules,
> which can be used as packaged dependencies, and refer to *internal* modules
> using relative paths, like "./foo.js". (If you have so many of these that
> it's a real problem, then your program might be doing too many things in one
> place.)
>
> The cost of violating convention is probably higher than the cost of a few
> dots in a few require() statements.
>
>
>
> On Saturday, February 9, 2013, Geerten van Meel wrote:
>>
>> Thank you, this was very helpful. Putting a plain .js file in that
>> directory distinguishes it sufficiently from other npm packages. However,
>> instead of putting the actual component file in there I opt to add a new
>> file ./node_modules/component.js containing the following single statement
>> in it to load the component:
>>
>> exports = module.exports = require('../path/to/component.js');
>>
>>
>> I know that some may facepalm, but I really like to distinguish own app
>> code from npm dependencies. Plain .js files are indeed both programmatically
>> (npm commands) and visually sufficiently different from package folders.
>>
>> Thank you everyone for your suggestions!
>>
>>
>> On Saturday, February 9, 2013 3:02:12 AM UTC+1, Isaac Schlueter wrote:
>>>
>>> I don't think it's a hack. Rather elegant, really:
>>>
>>> mkdir node_modules
>>> mv path/to/component.js node_modules/component.js
>>> node -pe "require('component') // voila"
>>>
>>> You could also make it a full-fledged package (jus a matter of adding
>>> one extra file package.json, which npm can do for you, though a README
>>> and LICENSE are strongly encouraged as well), and then list it as a
>>> dependency. That's the more "traditional" way that people handle
>>> dependencies like this. npm ignores anything in node_modules that
>>> isn't a package, so it should be fine just leaving it there,
>>> otherwise.
>>>
>>>
>>> On Fri, Feb 8, 2013 at 5:24 PM, Geerten van Meel <[email protected]>
>>> wrote:
>>> > Hi,
>>> >
>>> > I am working on an app and would like to access a component of my app
>>> > simply
>>> > using require('component') instead of require('./component') or worse
>>> > require('./path/to/component.js'). I know that there are ways to
>>> > achieve
>>> > this by doing some hack regarding the node_modules folder, but I would
>>> > like
>>> > to ask whether there is a simple, elegant way to achieve this result
>>> > (and
>>> > not interfere with npm when deploying)?
>>> >
>>> > To summarize:
>>> >
>>> > - I would like to include an app component using require('component')
>>> > instead of require('./path/to/component.js').
>>> >
>>> > - The components location should ideally be somewhere out of
>>> > node_modules
>>> > in order to not mess with npm.
>>> >
>>> > - NODE_PATH, thus process arguments should not be provided via the
>>> > command
>>> > line.
>>> >
>>> > Thank you for your time and suggestions!
>>> >
>>> > Geerten
>>> >
>>> > --
>>> > --
>>> > 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 [email protected]
>>> > To unsubscribe from this group, send email to
>>> > [email protected]
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/nodejs?hl=en?hl=en
>>> >
>>> > ---
>>> > You received this message because you are subscribed to the Google
>>> > Groups
>>> > "nodejs" group.
>>> > To unsubscribe from this group and stop receiving emails from it, send
>>> > an
>>> > email to [email protected].
>>> > For more options, visit https://groups.google.com/groups/opt_out.
>>> >
>>> >
>
> --
> --
> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.