diff options
author | Trevor Burnham <trevorburnham@gmail.com> | 2011-05-01 14:03:08 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2011-05-09 10:29:09 -0700 |
commit | 31ed87b0a964a9989c56a6e08906b96c492186ee (patch) | |
tree | cecbf5985d4eb2ca2977f5d2118121b26ea78798 /doc | |
parent | 110f06578da9fa4f5389269e9c86e56558a2d7dc (diff) | |
download | android-node-v8-31ed87b0a964a9989c56a6e08906b96c492186ee.tar.gz android-node-v8-31ed87b0a964a9989c56a6e08906b96c492186ee.tar.bz2 android-node-v8-31ed87b0a964a9989c56a6e08906b96c492186ee.zip |
Documenting `require.main`, fixing #997
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/modules.markdown | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/doc/api/modules.markdown b/doc/api/modules.markdown index 07ff31fe54..89490ebafc 100644 --- a/doc/api/modules.markdown +++ b/doc/api/modules.markdown @@ -305,6 +305,21 @@ the same process. As the application stack grows, we tend to assemble functionality, and it is a problem with those parts interact in ways that are difficult to predict. +### Accessing the main module + +When a file is run directly from Node, `require.main` is set to its +`module`. That means that you can determine whether a file has been run +directly by testing + + require.main === module + +For a file `foo.js`, this will be `true` if run via `node foo.js`, but +`false` if run by `require('./foo')`. + +Because `module` provides a `filename` property (normally equivalent to +`__filename`), the entry point of the current application can be obtained +by checking `require.main.filename`. + ## Addenda: Package Manager Tips The semantics of Node's `require()` function were designed to be general |