avoid relative path in require to include local modules

159 views
Skip to first unread message

Chetan Dhembre

unread,
Apr 22, 2014, 4:08:52 AM4/22/14
to nod...@googlegroups.com
Hi,
   I recently create module. Which help in avoiding mess of relative path while including local modules.I post module link and ask suggestion on #node.js irc center many people think it is basically wrong to have nested directory structure ( i also know it is dirty). So i was searching around in some big project in node.js . I come across  ghost which is fairly large node.js project. I end up looking their directory structure which is more or less nested .. i find following code


var fs = require('fs'),
    config = require('../../server/config')

which requiring local module using relative path and there is no second thought is this is error prone.

My question how does other large project manage code .. because there so many small piece code which are closely related to project so can not publish on npm and creating that number or private repo is not financially feasible ( talking about github)

Can any one tell how people manage large code base ?

Aria Stewart

unread,
Apr 22, 2014, 10:38:08 AM4/22/14
to nod...@googlegroups.com
On Tue, Apr 22, 2014 at 01:08:52AM -0700, Chetan Dhembre wrote:
> I end up looking their directory structure which is more or less nested .. i
> find following code
>
> var fs = require('fs'),
> > config = require('../../server/config')
> >
>
> which requiring local module using relative path and there is no second
> thought is this is error prone.

I don't see this as problematic: If you're not burying requires in late,
conditional code (which is an antipattern because blocking the runtime is no
fun), you'd discover this error early, if you make it at all.

It DOES require a paradigim shift if you're used to something like a PHP
autoloader, where location is irrelevant, or something like Java's import that
abstracts away from the notion of directories to some degree, but I don't think
it introduces any new kinds of breakage.

In this specific case, perhaps your module should have its configuration
injected by what requires it, rather than requiring its own configuration:

var fs = require('fs');
module.exports = function myModule(config) {
};

And where it's assembled:

var config = require('./server/config');
var mod = require('./modules/myModule.js')(config);

I think re-arranging to make your dependencies be both a directed, acyclic
graph (which it is now for you, since config doesn't require further things)
and making the directory structure map to that 1:1 as much as possible, you'll
find that things are easier to keep straight and you'll build less
interdependency.

> My question how does other large project manage code .. because there so
> many small piece code which are closely related to project so can not
> publish on npm and creating that number or private repo is not financially
> feasible (talking about github)
>
> Can any one tell how people manage large code base ?

Break things up into small modules -- commit them in node_modules, or isolate
them into better groups (so you don't have to do deep requires). If github's
pricing model bites you, consider using your own git repositories. A simple
http server will suffice, as will a git daemon.

It may in fact be a sign that you're designing something too tightly coupled
and you need to refactor better shapes.

Aria

Chetan Dhembre

unread,
Apr 22, 2014, 1:05:14 PM4/22/14
to nod...@googlegroups.com

I created module (local-require) which simple create symbolic link of ur local module to node_modules folder so that you can use them directly from any place from codebase.

Aria Stewart

unread,
Apr 22, 2014, 1:12:22 PM4/22/14
to nod...@googlegroups.com

On Apr 22, 02014, at 13:05, Chetan Dhembre <chetan...@gmail.com> wrote:

> > Can any one tell how people manage large code base ?
>
> Break things up into small modules -- commit them in node_modules, or isolate

I created module (local-require) which simple create symbolic link of ur local module to node_modules folder so that you can use them directly from any place from codebase.



That works with node as-is:

in node_modules/test/c.js:

    module.exports = “HELLO”;

in test.js, no matter how deep into the directory structure sibling to node_modules:

    console.log(require(‘test/c.js’));

logs “HELLO”.
signature.asc

Chetan Dhembre

unread,
Apr 22, 2014, 1:34:02 PM4/22/14
to nod...@googlegroups.com

Most of time node_modules folder is added in .gitignore .. and adding these local module in node_modules after making. Chages is painful.. thats way i created the module .. btw i know it is not ideal solution.. ideal scenario should not create  nested directory structure.. just in search of some good blog post which will explain it with fairly complex problem .. or about how to organize code in node.js project

Aria Stewart

unread,
Apr 22, 2014, 1:47:21 PM4/22/14
to nod...@googlegroups.com

On Apr 22, 02014, at 13:34, Chetan Dhembre <chetan...@gmail.com> wrote:

> Most of time node_modules folder is added in .gitignore
>
You could not do that ;-)

Seems the simplest solution.

> .. and adding these local module in node_modules after making. Chages is painful.. thats way i created the module .. btw i know it is not ideal solution.. ideal scenario should not create nested directory structure..

I think nesting is good — but I try hard to keep nesting modular, rather than arbitrary. You won’t find much “js/”, “css/”, “images/” in my projects for this reason, but you might find “login/”, “billing/”, etc. — they’re modules that talk mostly or entirely to themselves.

> just in search of some good blog post which will explain it with fairly complex problem .. or about how to organize code in node.js project

I think part of the problem is that there’s no canonical node.js project: Several frameworks have some rough ideas about structure, but node is more general.

If you want good advice on how to organize, you might have to be a lot more specific. Node’s not just for the web, you know!

Aria
signature.asc

// ravi

unread,
Apr 22, 2014, 9:00:51 PM4/22/14
to nod...@googlegroups.com
On Apr 22, 2014, at 4:08 AM, Chetan Dhembre <chetan...@gmail.com> wrote:
   I recently create module. Which help in avoiding mess of relative path while including local modules.I post module link and ask suggestion on #node.js irc center many people think it is basically wrong to have nested directory structure ( i also know it is dirty).


I am puzzled by why anyone would think nested directory structure is wrong or dirty. Did they explain why this is a bad idea? I’d say quite the opposite (for the same reasons why hierarchical directories exist in the operating system :-)).


So i was searching around in some big project in node.js . I come across  ghost which is fairly large node.js project. I end up looking their directory structure which is more or less nested .. i find following code

var fs = require('fs'),
    config = require('../../server/config')

which requiring local module using relative path and there is no second thought is this is error prone.

My question how does other large project manage code .. because there so many small piece code which are closely related to project so can not publish on npm and creating that number or private repo is not financially feasible ( talking about github)


I use NODE_PATH (in Unix, not sure what the equivalent is in Windows) to remove the “.." parts in the require() path. There used to be a way to specify this inline in the code, using require.paths (IIRC), which was really nice, but for some reason (that I now forget) that was removed. Something like this:

======

/home/ravi/code/projectX/app.js:

var fs = require(‘fs’),
    config = require(‘server/config’);

$ daemon ….. —env=“NODE_PATH=/home/ravi/code/projectX/server:…other local module paths…” /home/ravi/code/projectX/app.js

======

This of course still uses a relative path (‘server/config’) but that I consider a feature that exposes the internal hierarchy of my code.

Note: I am using ‘daemon’ to daemonise app.js (so it runs long after I have left the shell). There are other ways to do that and you may not need that at all, and might instead prefer writing a shell wrapper that sets NODE_PATH and then runs app.js.

Cheers,

—ravi

Karl Tiedt

unread,
Apr 22, 2014, 9:03:53 PM4/22/14
to nod...@googlegroups.com
I was just wondering the same thing... if your module is published in NPM or not... unless someone forcibly modifies your modules directory structure then relative paths INSIDE your module should be perfectly fine....

-Karl Tiedt


--
--
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
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
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 nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Floby

unread,
Apr 23, 2014, 4:34:30 AM4/23/14
to nod...@googlegroups.com
require.paths was removed because it could interfere with how modules are cached. Depending on the current value of require.paths, the same string could resolve to a different file at different times.

It was decided that modules should be local and that '../' is only three characters long. If you get it wrong, you'll now it soon enough.

I understand where your module comes from but it is a paradigm that was abandonned some time ago in the node community. In node, modules are local, either in your project's directories or in your project's node_modules. Splitting your project into several files is good, but knowing how and where to split can be hard.
Reply all
Reply to author
Forward
0 new messages