Permalink
Browse files

fix lookup when a module requires another module

if module A has a node_modules folder with module B, it should be able
    to require B and have that work as expected.
  • Loading branch information...
1 parent ba71a72 commit 661d0dff1663372e88269454035613987dc5070e @defunctzombie defunctzombie committed Oct 29, 2012
View
11 index.js
@@ -69,10 +69,12 @@ function from_source(source, parent, cb) {
return cb(new Error('unable to find module: ' + req));
}
+ var paths = parent.paths.concat(node_module_paths(full_path));
+
var new_parent = {
id: id,
filename: full_path,
- paths: parent.paths
+ paths: paths
}
from_filename(full_path, new_parent, function(err, deps) {
@@ -126,11 +128,16 @@ function lookup_path(name, parent) {
return priv_module.Module._findPath(name, paths);
}
+/// return an array of node_module paths given a filename
+function node_module_paths(filename) {
+ return priv_module.Module._nodeModulePaths(path.dirname(filename));
+}
+
/// process filename and callback with tree of dependencies
/// the tree does have circular references when a child requires a parent
module.exports = function(filename, cb) {
- var paths = priv_module.Module._nodeModulePaths(path.dirname(filename));
+ var paths = node_module_paths(filename);
// entry parent specifies the base node modules path
var entry_parent = {
View
1 test/fixtures/nested_module.js
@@ -0,0 +1 @@
+require('modb');
View
13 test/fixtures/nested_module.js.dep
@@ -0,0 +1,13 @@
+[
+ {
+ "id": "modb",
+ "filename": "node_modules/modb/index.js",
+ "deps": [
+ {
+ "id": "modc",
+ "filename": "node_modules/modb/node_modules/modc.js",
+ "deps": []
+ }
+ ]
+ }
+]
View
1 test/fixtures/node_modules/modb/index.js
@@ -0,0 +1 @@
+require('modc');
View
1 test/fixtures/node_modules/modb/node_modules/modc.js
@@ -0,0 +1 @@
+// another dummy!!

0 comments on commit 661d0df

Please sign in to comment.