Merge pull request #3 from strongloop/feature/fix-requires-of-nested-modules

Fix loading of loopback dependencies.
This commit is contained in:
Miroslav Bajtoš 2013-12-09 08:15:32 -08:00
commit 6040be6ff3
1 changed files with 23 additions and 2 deletions

View File

@ -3,8 +3,8 @@
*/
var path = require('path');
var loopback = require('loopback');
var swagger = require('loopback/node_modules/strong-remoting/ext/swagger');
var express = require('loopback/node_modules/express');
var swagger = requireLoopbackDependency('strong-remoting/ext/swagger');
var express = requireLoopbackDependency('express');
var STATIC_ROOT = path.join(__dirname, 'public');
module.exports = explorer;
@ -30,3 +30,24 @@ function explorer(loopbackApplication, options) {
app.use(loopback.static(STATIC_ROOT));
return app;
}
function requireLoopbackDependency(module) {
try {
return require('loopback/node_modules/' + module);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err;
try {
// Dependencies may be installed outside the loopback module,
// e.g. as peer dependencies. Try to load the dependency from there.
return require(module);
} catch (errPeer) {
if (errPeer.code !== 'MODULE_NOT_FOUND') throw errPeer;
// Rethrow the initial error to make it clear that we were trying
// to load a module that should have been installed inside
// "loopback/node_modules". This should minimise end-user's confusion.
// However, such situation should never happen as `require('loopback')`
// would have failed before this function was even called.
throw err;
}
}
}