@@ -68,8 +68,7 @@ parent directory of the current module, and adds `/node_modules`, and
6868attempts to load the module from that location.
6969
7070If 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
7473For 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:
8382This allows programs to localize their dependencies, so that they do not
8483clash.
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
11087It 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
331308by 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
335327The semantics of Node's ` require() ` function were designed to be general
0 commit comments