Merge remote-tracking branch 'origin/v0.8'
[platform/upstream/nodejs.git] / doc / api / globals.markdown
1 # Global Objects
2
3 <!-- type=misc -->
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.
7
8 ## global
9
10 <!-- type=global -->
11
12 * {Object} The global namespace object.
13
14 In browsers, the top-level scope is the global scope. That means that in
15 browsers if you're in the global scope `var something` will define a global
16 variable. In Node this is different. The top-level scope is not the global
17 scope; `var something` inside a Node module will be local to that module.
18
19 ## process
20
21 <!-- type=global -->
22
23 * {Object}
24
25 The process object. See the [process object][] section.
26
27 ## console
28
29 <!-- type=global -->
30
31 * {Object}
32
33 Used to print to stdout and stderr. See the [stdio][] section.
34
35 ## Class: Buffer
36
37 <!-- type=global -->
38
39 * {Function}
40
41 Used to handle binary data. See the [buffer section][]
42
43 ## require()
44
45 <!-- type=var -->
46
47 * {Function}
48
49 To require modules. See the [Modules][] section.  `require` isn't actually a
50 global but rather local to each module.
51
52 ### require.resolve()
53
54 Use the internal `require()` machinery to look up the location of a module,
55 but rather than loading the module, just return the resolved filename.
56
57 ### require.cache
58
59 * {Object}
60
61 Modules are cached in this object when they are required. By deleting a key
62 value from this object, the next `require` will reload the module.
63
64 ### require.extensions
65
66 * {Array}
67
68 Instruct `require` on how to handle certain file extensions.
69
70 Process files with the extension `.sjs` as `.js`:
71
72     require.extensions['.sjs'] = require.extensions['.js'];
73
74 Write your own extension handler:
75
76     require.extensions['.sjs'] = function(module, filename) {
77       var content = fs.readFileSync(filename, 'utf8');
78       // Parse the file content and give to module.exports
79       module.exports = content;
80     };
81
82 ## __filename
83
84 <!-- type=var -->
85
86 * {String}
87
88 The filename of the code being executed.  This is the resolved absolute path
89 of this code file.  For a main program this is not necessarily the same
90 filename used in the command line.  The value inside a module is the path
91 to that module file.
92
93 Example: running `node example.js` from `/Users/mjr`
94
95     console.log(__filename);
96     // /Users/mjr/example.js
97
98 `__filename` isn't actually a global but rather local to each module.
99
100 ## __dirname
101
102 <!-- type=var -->
103
104 * {String}
105
106 The name of the directory that the currently executing script resides in.
107
108 Example: running `node example.js` from `/Users/mjr`
109
110     console.log(__dirname);
111     // /Users/mjr
112
113 `__dirname` isn't actually a global but rather local to each module.
114
115
116 ## module
117
118 <!-- type=var -->
119
120 * {Object}
121
122 A reference to the current module. In particular
123 `module.exports` is the same as the `exports` object.
124 `module` isn't actually a global but rather local to each module.
125
126 See the [module system documentation][] for more information.
127
128 ## exports
129
130 <!-- type=var -->
131
132 An object which is shared between all instances of the current module and
133 made accessible through `require()`.
134 `exports` is the same as the `module.exports` object.
135 `exports` isn't actually a global but rather local to each module.
136
137 See the [module system documentation][] for more information.
138
139 See the [module section][] for more information.
140
141 ## setTimeout(cb, ms)
142
143 Run callback `cb` after *at least* `ms` milliseconds. The actual delay depends
144 on external factors like OS timer granularity and system load.
145
146 The timeout must be in the range of 1-2,147,483,647 inclusive. If the value is
147 outside that range, it's changed to 1 millisecond. Broadly speaking, a timer
148 cannot span more than 24.8 days.
149
150 Returns an opaque value that represents the timer.
151
152 ## clearTimeout(t)
153
154 Stop a timer that was previously created with `setTimeout()`. The callback will
155 not execute.
156
157 ## setInterval(cb, ms)
158
159 Run callback `cb` repeatedly every `ms` milliseconds. Note that the actual
160 interval may vary, depending on external factors like OS timer granularity and
161 system load. It's never less than `ms` but it may be longer.
162
163 The interval must be in the range of 1-2,147,483,647 inclusive. If the value is
164 outside that range, it's changed to 1 millisecond. Broadly speaking, a timer
165 cannot span more than 24.8 days.
166
167 Returns an opaque value that represents the timer.
168
169 ## clearInterval(t)
170
171 Stop a timer that was previously created with `setInterval()`. The callback
172 will not execute.
173
174 <!--type=global-->
175
176 The timer functions are global variables. See the [timers][] section.
177
178 [buffer section]: buffer.html
179 [module section]: modules.html
180 [module system documentation]: modules.html
181 [Modules]: modules.html#modules_modules
182 [process object]: process.html#process_process
183 [stdio]: stdio.html
184 [timers]: timers.html