3

I ran into an issue with my Nodejs application. I have two different apps that are using shared library, which is located so that it is found one level up in node_modules. So i have this structure ./app1/app.js, ./app2/app.js and ./node_modules/shared.libs/index.js.

shared.libs in its turn has some other modules installed, like mongoose, redis etc. Plus some mogoose models with additional functions in them. All are exported from index.js like this:

exports.async = require('async');
exports.config = require('./config');
exports.utils = require('./lib/utils');

And then in apps i import them like this:

var libs = require('shared.libs');
var config = libs.config;

So after this code i can use config which is coming from that shared library. This part was and is working just fine. But now i need to put additional layer on top of this library (read: provide more unified interface for both apps). What i tried to do is to add some functions into index.js of shared library and then export the whole object with these functions. But whenever i try to call previously imported (by var libs = require('shared.libs');) object it says that libs is not defined.

What am i doing wrong here?

I generally want to keep other code the same, so i won't need to go over replacing require part everywhere, but at the same time provide additional functionality from shared library which will be available from that imported libs object.

6
  • 1
    Show some more code - how you're trying to export the new functions, where is the require that is not working, etc. Commented Oct 11, 2012 at 10:38
  • @IvanVergiliev The code of how i export it and how i require is pretty much explained. How could i improve it? I have tried directly exporting function like exports.test = function(){console.log("test");} and then require it from another place, also tried creating new object which implements all needed functions and export it. Both ways are not working. Whenever i try to call libs.test() it says me that libs is undefined. And whenever i check libs object via node-inspector it gives me same output. Commented Oct 12, 2012 at 5:52
  • 1
    So, you're saying that as long as you only have exports.sth = require(module), it's all good, but if you also add exports.fun = function() {}, the whole module stops working, is that correct? Doesn't sound quite right. Commented Oct 12, 2012 at 10:45
  • @IvanVergiliev Nope, if add exports.fun = function(){} this only function does not work. But all other things exported in that shared library are still working. I just meant strange behavior that if i check lib itself it is undefined. Commented Oct 15, 2012 at 7:14
  • 1
    Maybe you should try the exercise of finding the minimum amount of code that reproduces the error you're seeing. Then you'll either be able to see the problem yourself, as there will be much less code, or you'll be able to post the code here and get better answers. Commented Oct 15, 2012 at 10:47

1 Answer 1

7
+50

this should definitely work:

module.exports = {
  async: require('async'),
  config: require('./config'),
  utils: require('./lib/utils'),
  foo: function () {
    return 'bar';
  }
};

reference like:

var libs = require('shared.libs');

console.log(libs.async);
console.log(libs.config);
console.log(libs.utils);
console.log(libs.foo);
console.log(libs.foo());

What makes me wonder is one of your comments above, that you get an error libs is not defined. That looks like you should have other unnoticed errors before.. during the the initialization of your shared.libs module..

Sign up to request clarification or add additional context in comments.

4 Comments

This does work, but as i mentioned, i need solution where i won't need to go over all files changing require part everywhere. So if it was like var libs = require('shared.libs'); var config = libs.config; Then it should stay this way. All i want is to be able call to libs object, which it gives me undefined
the interface stays perfectly as it was.. updated reference example in my post to show you
I tried this, but seems not working. I am still getting libs is not defined. Which is awkward.
if you want me to have a look post me this 'shared.lib'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.