2
2
3
3
<!-- type=misc -->
4
4
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() ` ] [ ]
7
14
8
15
The objects listed here are specific to Node.js. There are a number of
9
16
[ built-in objects] [ ] that are part of the JavaScript language itself, which are
@@ -21,67 +28,12 @@ added: v0.1.103
21
28
Used to handle binary data. See the [ buffer section] [ ] .
22
29
23
30
## \_\_ 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.
36
31
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 ` ] .
45
33
46
34
## \_\_ 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.
60
35
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 ` ] .
85
37
86
38
## clearImmediate(immediateObject)
87
39
<!-- YAML
@@ -122,19 +74,8 @@ added: v0.1.100
122
74
Used to print to stdout and stderr. See the [ ` console ` ] [ ] section.
123
75
124
76
## 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.
136
77
137
- See the [ module system documentation ] [ ] for more information .
78
+ This variable may appear to be global but is not. See [ ` exports ` ] .
138
79
139
80
## global
140
81
<!-- YAML
@@ -151,21 +92,8 @@ Node.js this is different. The top-level scope is not the global scope;
151
92
` var something ` inside a Node.js module will be local to that module.
152
93
153
94
## module
154
- <!-- YAML
155
- added: v0.1.16
156
- -->
157
95
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 ` ] .
169
97
170
98
## process
171
99
<!-- YAML
@@ -179,71 +107,8 @@ added: v0.1.7
179
107
The process object. See the [ ` process ` object] [ ] section.
180
108
181
109
## 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
- -->
244
110
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() ` ] .
247
112
248
113
## setImmediate(callback[ , ...args] )
249
114
<!-- YAML
@@ -272,20 +137,20 @@ added: v0.0.1
272
137
273
138
[ ` setTimeout ` ] is described in the [ timers] [ ] section.
274
139
275
- [ `__dirname` ] : #globals_dirname
276
- [ `__filename` ] : #globals_filename
140
+ [ `__dirname` ] : modules.html#modules_dirname
141
+ [ `__filename` ] : modules.html#modules_filename
277
142
[ `clearImmediate` ] : timers.html#timers_clearimmediate_immediate
278
143
[ `clearInterval` ] : timers.html#timers_clearinterval_timeout
279
144
[ `clearTimeout` ] : timers.html#timers_cleartimeout_timeout
280
145
[ `console` ] : console.html
281
- [ `path.dirname()` ] : path.html#path_path_dirname_path
146
+ [ `exports` ] : modules.html#modules_exports
147
+ [ `module` ] : modules.html#modules_module
282
148
[ `process` object ] : process.html#process_process
149
+ [ `require()` ] : modules.html#modules_require
283
150
[ `setImmediate` ] : timers.html#timers_setimmediate_callback_args
284
151
[ `setInterval` ] : timers.html#timers_setinterval_callback_delay_args
285
152
[ `setTimeout` ] : timers.html#timers_settimeout_callback_delay_args
286
- [ Modules ] : modules.html#modules_modules
287
153
[ buffer section ] : buffer.html
288
154
[ built-in objects ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
289
155
[ module system documentation ] : modules.html
290
- [ native addons ] : addons.html
291
156
[ timers ] : timers.html
0 commit comments