Reputation: 2948
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.
Upvotes: 3
Views: 5326
Reputation: 2615
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..
Upvotes: 7