Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Closes #1177 remove one node_modules optimization
Browse files Browse the repository at this point in the history
to better support certain project structures.
  • Loading branch information
Mathias Buus authored and isaacs committed Jun 14, 2011
1 parent 88552c5 commit 39246f6
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
25 changes: 1 addition & 24 deletions doc/api/modules.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions test/fixtures/node_modules/baz/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39246f6

Please sign in to comment.