Skip to content

Commit 2ba706d

Browse files
author
Mark Cavage
committed
Merge branch 'v0.4' of git://github.com/joyent/node into v0.4
2 parents b6d7f2a + 39246f6 commit 2ba706d

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed

doc/api/modules.markdown

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ parent directory of the current module, and adds `/node_modules`, and
6868
attempts to load the module from that location.
6969

7070
If it is not found there, then it moves to the parent directory, and so
71-
on, until either the module is found, or the root of the tree is
72-
reached.
71+
on, until the root of the tree is reached.
7372

7473
For example, if the file at `'/home/ry/projects/foo.js'` called
7574
`require('bar.js')`, then node would look in the following locations, in
@@ -83,28 +82,6 @@ this order:
8382
This allows programs to localize their dependencies, so that they do not
8483
clash.
8584

86-
#### Optimizations to the `node_modules` Lookup Process
87-
88-
When there are many levels of nested dependencies, it is possible for
89-
these file trees to get fairly long. The following optimizations are thus
90-
made to the process.
91-
92-
First, `/node_modules` is never appended to a folder already ending in
93-
`/node_modules`.
94-
95-
Second, if the file calling `require()` is already inside a `node_modules`
96-
hierarchy, then the top-most `node_modules` folder is treated as the
97-
root of the search tree.
98-
99-
For example, if the file at
100-
`'/home/ry/projects/foo/node_modules/bar/node_modules/baz/quux.js'`
101-
called `require('asdf.js')`, then node would search the following
102-
locations:
103-
104-
* `/home/ry/projects/foo/node_modules/bar/node_modules/baz/node_modules/asdf.js`
105-
* `/home/ry/projects/foo/node_modules/bar/node_modules/asdf.js`
106-
* `/home/ry/projects/foo/node_modules/asdf.js`
107-
10885
### Folders as Modules
10986

11087
It is convenient to organize programs and libraries into self-contained
@@ -330,6 +307,21 @@ Because `module` provides a `filename` property (normally equivalent to
330307
`__filename`), the entry point of the current application can be obtained
331308
by checking `require.main.filename`.
332309

310+
### Accessing the main module
311+
312+
When a file is run directly from Node, `require.main` is set to its
313+
`module`. That means that you can determine whether a file has been run
314+
directly by testing
315+
316+
require.main === module
317+
318+
For a file `foo.js`, this will be `true` if run via `node foo.js`, but
319+
`false` if run by `require('./foo')`.
320+
321+
Because `module` provides a `filename` property (normally equivalent to
322+
`__filename`), the entry point of the current application can be obtained
323+
by checking `require.main.filename`.
324+
333325
## Addenda: Package Manager Tips
334326

335327
The semantics of Node's `require()` function were designed to be general

lib/module.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,7 @@ Module._nodeModulePaths = function(from) {
199199
var paths = [];
200200
var parts = from.split(splitRe);
201201

202-
var root = parts.indexOf('node_modules') - 1;
203-
if (root < 0) root = 0;
204-
205-
var tip = parts.length - 1;
206-
207-
for (var tip = parts.length - 1; tip >= root; tip --) {
202+
for (var tip = parts.length - 1; tip >= 0; tip --) {
208203
// don't search in .../node_modules/node_modules
209204
if (parts[tip] === 'node_modules') continue;
210205
var dir = parts.slice(0, tip + 1).concat('node_modules').join(joiner);

lib/util.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ exports.pump = function(readStream, writeStream, callback) {
423423
exports.inherits = function(ctor, superCtor) {
424424
ctor.super_ = superCtor;
425425
ctor.prototype = Object.create(superCtor.prototype, {
426-
constructor: { value: ctor, enumerable: false }
426+
constructor: {
427+
value: ctor,
428+
enumerable: false,
429+
writable: true,
430+
configurable: true
431+
}
427432
});
428433
};

test/fixtures/node_modules/baz/index.js

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)