Node module?

32 views
Skip to first unread message

Kevin Burton

unread,
Apr 18, 2014, 9:57:06 AM4/18/14
to nod...@googlegroups.com
This is probably a very basic question but I could not find a definitive answer right away so I am hoping someone on this group would be willing to provide me with some insight.

Right now I have an application that is split into various .js files. Each file performs a specific function. For example I invoke 'node presentation.js' and that makes various database and WebAPI calls to get information about a 'presentation'. There are about 20 of these types of JavaScript files and I could write a script to invoke each one of them like 'node xxx.js'. But what I would prefer to do is to have a single 'app.js' file that would contain something like the following lines:

var foo = require('foo');
foo.presentation();
foo.folder();
. . . .
etc

Then invoke this single file like 'node app.js'. In other words I would like a library. I have put in presentation.js and similar files an 'exports' like exports.presentation = function () { . . . .}; But my first hurdle is in the lines above I get an error from 'require('foo')' that indicates 'Cannot find module 'foo''. I know I can 'require('./presentation.js') and it will find the JavaScript file starting with the current folder but I would like more of a library. So to put the question succinctly I would like to know how to structure my folders so the require would pull in a library of functions each defined by a separate .js file? Pointers? Ideas?

Thank you.

Francesco Mari

unread,
Apr 18, 2014, 2:12:01 PM4/18/14
to nod...@googlegroups.com
You need to create separate projects for each of your modules, and
install each module via NPM. Please read the documentation about
modules [1] to understand how the modules are loaded. You may also
want to read one of the many tutorials about NPM.

One higher level consideration. You describe your application as
composed of different modules, each of them performing a specific task
in your application. If those modules are highly coupled, or if it
doesn't make sense to publish each of them as standalone modules, you
should probably re-think the design of your application.

[1]: http://nodejs.org/docs/latest/api/modules.html
> --
> --
> 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.

Kevin Burton

unread,
Apr 18, 2014, 2:44:40 PM4/18/14
to nod...@googlegroups.com
Thank you. I may have caused a misunderstanding. I have about 20 .js files that I would like to combine into a single module. I would like to do one require and have access to the functions detailed in each of the 20 files. Thus a library.

Aria Stewart

unread,
Apr 18, 2014, 2:51:03 PM4/18/14
to nod...@googlegroups.com

On Apr 18, 02014, at 14:44, Kevin Burton <ronald.ke...@gmail.com> wrote:

> Thank you. I may have caused a misunderstanding. I have about 20 .js files that I would like to combine into a single module. I would like to do one require and have access to the functions detailed in each of the 20 files. Thus a library.


module.exports = {
submodule: require(‘./submodule’),
sub2: require(‘./sub2’),
func3: require(‘./sub3’).func3,
func3a: require(‘./sub3’).func3a
};

You’ll have to export what you want from the submodules.

Consider breaking out things that aren’t actually related into their own packages.



signature.asc
Reply all
Reply to author
Forward
0 new messages