I'm trying to create a require wrapper to load dependencies, but I found one thing difficult to make it works as the original require function. When the path is a relative one, the wrapper cannot resolve to the correct one since my loader and the caller files are not in a same folder. Here is a simplified description.
index.js
lib/
loader.js
foo/
bar.js
baz.js
index.js
var loader = require('./lib/loader.js'),
bar = require('./foo/bar.js');
bar(loader);
lib/loader.js
module.exports = function (path) {
return require(path);
};
foo/bar.js
module.exports = function(loader) {
var baz = loader('./baz.js');
console.log(baz);
};
foo/baz.js
module.exports = 'baz';
Obviously, when the index.js is executed, baz.js file cannot be found. Is there any way to resolve to the correct file?
I've found a relative solution but it's not working.