Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Created June 13, 2011 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafintosh/1023916 to your computer and use it in GitHub Desktop.
Save mafintosh/1023916 to your computer and use it in GitHub Desktop.
Fix for issue #1177
From 5068d76a76eec4e72f3d59cc2eca0d87a97345f0 Mon Sep 17 00:00:00 2001
From: Mathias Buus <m@ge.tt>
Date: Tue, 14 Jun 2011 00:42:06 +0200
Subject: [PATCH] removed the node_modules optimization that made it achor its path by a node_modules folder to better support certain project structures. issue #1177
---
doc/api/modules.markdown | 25 +------------------------
lib/module.js | 7 +------
test/fixtures/node_modules/baz/index.js | 6 ------
3 files changed, 2 insertions(+), 36 deletions(-)
diff --git a/doc/api/modules.markdown b/doc/api/modules.markdown
index eec27b2..59f545e 100644
--- a/doc/api/modules.markdown
+++ b/doc/api/modules.markdown
@@ -68,8 +68,7 @@ parent directory of the current module, and adds `/node_modules`, and
attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and so
-on, until either the module is found, or the root of the tree is
-reached.
+on, until the root of the tree is reached.
For example, if the file at `'/home/ry/projects/foo.js'` called
`require('bar.js')`, then node would look in the following locations, in
@@ -83,28 +82,6 @@ this order:
This allows programs to localize their dependencies, so that they do not
clash.
-#### Optimizations to the `node_modules` Lookup Process
-
-When there are many levels of nested dependencies, it is possible for
-these file trees to get fairly long. The following optimizations are thus
-made to the process.
-
-First, `/node_modules` is never appended to a folder already ending in
-`/node_modules`.
-
-Second, if the file calling `require()` is already inside a `node_modules`
-hierarchy, then the top-most `node_modules` folder is treated as the
-root of the search tree.
-
-For example, if the file at
-`'/home/ry/projects/foo/node_modules/bar/node_modules/baz/quux.js'`
-called `require('asdf.js')`, then node would search the following
-locations:
-
-* `/home/ry/projects/foo/node_modules/bar/node_modules/baz/node_modules/asdf.js`
-* `/home/ry/projects/foo/node_modules/bar/node_modules/asdf.js`
-* `/home/ry/projects/foo/node_modules/asdf.js`
-
### Folders as Modules
It is convenient to organize programs and libraries into self-contained
diff --git a/lib/module.js b/lib/module.js
index 308fd4e..b459d94 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -199,12 +199,7 @@ Module._nodeModulePaths = function(from) {
var paths = [];
var parts = from.split(splitRe);
- var root = parts.indexOf('node_modules') - 1;
- if (root < 0) root = 0;
-
- var tip = parts.length - 1;
-
- for (var tip = parts.length - 1; tip >= root; tip --) {
+ for (var tip = parts.length - 1; tip >= 0; tip --) {
// don't search in .../node_modules/node_modules
if (parts[tip] === 'node_modules') continue;
var dir = parts.slice(0, tip + 1).concat('node_modules').join(joiner);
diff --git a/test/fixtures/node_modules/baz/index.js b/test/fixtures/node_modules/baz/index.js
index 44acc13..84f587f 100644
--- a/test/fixtures/node_modules/baz/index.js
+++ b/test/fixtures/node_modules/baz/index.js
@@ -25,11 +25,5 @@ console.error(module.paths.join('\n')+'\n');
var assert = require('assert');
assert.equal(require('bar'), require('../bar.js'));
-// since this is inside a node_modules folder,
-// it should be impossible to ever see /node_modules in the
-// lookup paths, since it's rooted on the uppermost node_modules
-// directory.
-assert.equal(-1, module.paths.indexOf('/node_modules'));
-
// this should work, and get the one in ./node_modules/asdf.js
assert.equal(require('asdf'), require('./node_modules/asdf.js'));
--
1.7.4.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment