22
33<!-- type=misc -->
44
5- These objects are available in all modules. Some of these objects aren't
6- actually in the global scope but in the module scope - this will be noted.
5+ These objects are available in all modules. The following variables may appear
6+ to be global but are not. They exist only in the scope of modules, see the
7+ [ module system documentation] [ ] :
8+
9+ - [ ` __dirname ` ] [ ]
10+ - [ ` __filename ` ] [ ]
11+ - [ ` exports ` ] [ ]
12+ - [ ` module ` ] [ ]
13+ - [ ` require() ` ] [ ]
714
815The objects listed here are specific to Node.js. There are a number of
916[ built-in objects] [ ] that are part of the JavaScript language itself, which are
@@ -21,67 +28,12 @@ added: v0.1.103
2128Used to handle binary data. See the [ buffer section] [ ] .
2229
2330## \_\_ dirname
24- <!-- YAML
25- added: v0.1.27
26- -->
27-
28- <!-- type=var -->
29-
30- * {string}
31-
32- The directory name of the current module. This the same as the
33- [ ` path.dirname() ` ] [ ] of the [ ` __filename ` ] [ ] .
34-
35- ` __dirname ` is not actually a global but rather local to each module.
3631
37- Example: running ` node example.js ` from ` /Users/mjr `
38-
39- ``` js
40- console .log (__dirname );
41- // Prints: /Users/mjr
42- console .log (path .dirname (__filename ));
43- // Prints: /Users/mjr
44- ```
32+ This variable may appear to be global but is not. See [ ` __dirname ` ] .
4533
4634## \_\_ filename
47- <!-- YAML
48- added: v0.0.1
49- -->
50-
51- <!-- type=var -->
52-
53- * {string}
54-
55- The file name of the current module. This is the resolved absolute path of the
56- current module file.
57-
58- For a main program this is not necessarily the same as the file name used in the
59- command line.
6035
61- See [ ` __dirname ` ] [ ] for the directory name of the current module.
62-
63- ` __filename ` is not actually a global but rather local to each module.
64-
65- Examples:
66-
67- Running ` node example.js ` from ` /Users/mjr `
68-
69- ``` js
70- console .log (__filename );
71- // Prints: /Users/mjr/example.js
72- console .log (__dirname );
73- // Prints: /Users/mjr
74- ```
75-
76- Given two modules: ` a ` and ` b ` , where ` b ` is a dependency of
77- ` a ` and there is a directory structure of:
78-
79- * ` /Users/mjr/app/a.js `
80- * ` /Users/mjr/app/node_modules/b/b.js `
81-
82- References to ` __filename ` within ` b.js ` will return
83- ` /Users/mjr/app/node_modules/b/b.js ` while references to ` __filename ` within
84- ` a.js ` will return ` /Users/mjr/app/a.js ` .
36+ This variable may appear to be global but is not. See [ ` __filename ` ] .
8537
8638## clearImmediate(immediateObject)
8739<!-- YAML
@@ -122,19 +74,8 @@ added: v0.1.100
12274Used to print to stdout and stderr. See the [ ` console ` ] [ ] section.
12375
12476## exports
125- <!-- YAML
126- added: v0.1.12
127- -->
128-
129- <!-- type=var -->
130-
131- A reference to the ` module.exports ` that is shorter to type.
132- See [ module system documentation] [ ] for details on when to use ` exports ` and
133- when to use ` module.exports ` .
134-
135- ` exports ` is not actually a global but rather local to each module.
13677
137- See the [ module system documentation ] [ ] for more information .
78+ This variable may appear to be global but is not. See [ ` exports ` ] .
13879
13980## global
14081<!-- YAML
@@ -151,21 +92,8 @@ Node.js this is different. The top-level scope is not the global scope;
15192` var something ` inside a Node.js module will be local to that module.
15293
15394## module
154- <!-- YAML
155- added: v0.1.16
156- -->
15795
158- <!-- type=var -->
159-
160- * {Object}
161-
162- A reference to the current module. In particular
163- ` module.exports ` is used for defining what a module exports and makes
164- available through ` require() ` .
165-
166- ` module ` is not actually a global but rather local to each module.
167-
168- See the [ module system documentation] [ ] for more information.
96+ This variable may appear to be global but is not. See [ ` module ` ] .
16997
17098## process
17199<!-- YAML
@@ -179,71 +107,8 @@ added: v0.1.7
179107The process object. See the [ ` process ` object] [ ] section.
180108
181109## require()
182- <!-- YAML
183- added: v0.1.13
184- -->
185-
186- <!-- type=var -->
187-
188- * {Function}
189-
190- To require modules. See the [ Modules] [ ] section. ` require ` is not actually a
191- global but rather local to each module.
192-
193- ### require.cache
194- <!-- YAML
195- added: v0.3.0
196- -->
197-
198- * {Object}
199-
200- Modules are cached in this object when they are required. By deleting a key
201- value from this object, the next ` require ` will reload the module. Note that
202- this does not apply to [ native addons] [ ] , for which reloading will result in an
203- Error.
204-
205- ### require.extensions
206- <!-- YAML
207- added: v0.3.0
208- deprecated: v0.10.6
209- -->
210-
211- > Stability: 0 - Deprecated
212-
213- * {Object}
214-
215- Instruct ` require ` on how to handle certain file extensions.
216-
217- Process files with the extension ` .sjs ` as ` .js ` :
218-
219- ``` js
220- require .extensions [' .sjs' ] = require .extensions [' .js' ];
221- ```
222-
223- ** Deprecated** In the past, this list has been used to load
224- non-JavaScript modules into Node.js by compiling them on-demand.
225- However, in practice, there are much better ways to do this, such as
226- loading modules via some other Node.js program, or compiling them to
227- JavaScript ahead of time.
228-
229- Since the module system is locked, this feature will probably never go
230- away. However, it may have subtle bugs and complexities that are best
231- left untouched.
232-
233- Note that the number of file system operations that the module system
234- has to perform in order to resolve a ` require(...) ` statement to a
235- filename scales linearly with the number of registered extensions.
236-
237- In other words, adding extensions slows down the module loader and
238- should be discouraged.
239-
240- ### require.resolve()
241- <!-- YAML
242- added: v0.3.0
243- -->
244110
245- Use the internal ` require() ` machinery to look up the location of a module,
246- but rather than loading the module, just return the resolved filename.
111+ This variable may appear to be global but is not. See [ ` require() ` ] .
247112
248113## setImmediate(callback[ , ...args] )
249114<!-- YAML
@@ -272,20 +137,20 @@ added: v0.0.1
272137
273138[ ` setTimeout ` ] is described in the [ timers] [ ] section.
274139
275- [ `__dirname` ] : #globals_dirname
276- [ `__filename` ] : #globals_filename
140+ [ `__dirname` ] : modules.html#modules_dirname
141+ [ `__filename` ] : modules.html#modules_filename
277142[ `clearImmediate` ] : timers.html#timers_clearimmediate_immediate
278143[ `clearInterval` ] : timers.html#timers_clearinterval_timeout
279144[ `clearTimeout` ] : timers.html#timers_cleartimeout_timeout
280145[ `console` ] : console.html
281- [ `path.dirname()` ] : path.html#path_path_dirname_path
146+ [ `exports` ] : modules.html#modules_exports
147+ [ `module` ] : modules.html#modules_module
282148[ `process` object ] : process.html#process_process
149+ [ `require()` ] : modules.html#modules_require
283150[ `setImmediate` ] : timers.html#timers_setimmediate_callback_args
284151[ `setInterval` ] : timers.html#timers_setinterval_callback_delay_args
285152[ `setTimeout` ] : timers.html#timers_settimeout_callback_delay_args
286- [ Modules ] : modules.html#modules_modules
287153[ buffer section ] : buffer.html
288154[ built-in objects ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
289155[ module system documentation ] : modules.html
290- [ native addons ] : addons.html
291156[ timers ] : timers.html
0 commit comments