1# Global objects 2 3<!--introduced_in=v0.10.0--> 4<!-- type=misc --> 5 6These objects are available in all modules. The following variables may appear 7to be global but are not. They exist only in the scope of modules, see the 8[module system documentation][]: 9 10* [`__dirname`][] 11* [`__filename`][] 12* [`exports`][] 13* [`module`][] 14* [`require()`][] 15 16The objects listed here are specific to Node.js. There are [built-in objects][] 17that are part of the JavaScript language itself, which are also globally 18accessible. 19 20## Class: `Buffer` 21<!-- YAML 22added: v0.1.103 23--> 24 25<!-- type=global --> 26 27* {Function} 28 29Used to handle binary data. See the [buffer section][]. 30 31## `__dirname` 32 33This variable may appear to be global but is not. See [`__dirname`][]. 34 35## `__filename` 36 37This variable may appear to be global but is not. See [`__filename`][]. 38 39## `clearImmediate(immediateObject)` 40<!-- YAML 41added: v0.9.1 42--> 43 44<!--type=global--> 45 46[`clearImmediate`][] is described in the [timers][] section. 47 48## `clearInterval(intervalObject)` 49<!-- YAML 50added: v0.0.1 51--> 52 53<!--type=global--> 54 55[`clearInterval`][] is described in the [timers][] section. 56 57## `clearTimeout(timeoutObject)` 58<!-- YAML 59added: v0.0.1 60--> 61 62<!--type=global--> 63 64[`clearTimeout`][] is described in the [timers][] section. 65 66## `console` 67<!-- YAML 68added: v0.1.100 69--> 70 71<!-- type=global --> 72 73* {Object} 74 75Used to print to stdout and stderr. See the [`console`][] section. 76 77## `exports` 78 79This variable may appear to be global but is not. See [`exports`][]. 80 81## `global` 82<!-- YAML 83added: v0.1.27 84--> 85 86<!-- type=global --> 87 88* {Object} The global namespace object. 89 90In browsers, the top-level scope is the global scope. This means that 91within the browser `var something` will define a new global variable. In 92Node.js this is different. The top-level scope is not the global scope; 93`var something` inside a Node.js module will be local to that module. 94 95## `module` 96 97This variable may appear to be global but is not. See [`module`][]. 98 99## `process` 100<!-- YAML 101added: v0.1.7 102--> 103 104<!-- type=global --> 105 106* {Object} 107 108The process object. See the [`process` object][] section. 109 110## `queueMicrotask(callback)` 111<!-- YAML 112added: v11.0.0 113--> 114 115<!-- type=global --> 116 117* `callback` {Function} Function to be queued. 118 119The `queueMicrotask()` method queues a microtask to invoke `callback`. If 120`callback` throws an exception, the [`process` object][] `'uncaughtException'` 121event will be emitted. 122 123The microtask queue is managed by V8 and may be used in a similar manner to 124the [`process.nextTick()`][] queue, which is managed by Node.js. The 125`process.nextTick()` queue is always processed before the microtask queue 126within each turn of the Node.js event loop. 127 128```js 129// Here, `queueMicrotask()` is used to ensure the 'load' event is always 130// emitted asynchronously, and therefore consistently. Using 131// `process.nextTick()` here would result in the 'load' event always emitting 132// before any other promise jobs. 133 134DataHandler.prototype.load = async function load(key) { 135 const hit = this._cache.get(url); 136 if (hit !== undefined) { 137 queueMicrotask(() => { 138 this.emit('load', hit); 139 }); 140 return; 141 } 142 143 const data = await fetchData(key); 144 this._cache.set(url, data); 145 this.emit('load', data); 146}; 147``` 148 149## `require()` 150 151This variable may appear to be global but is not. See [`require()`][]. 152 153## `setImmediate(callback[, ...args])` 154<!-- YAML 155added: v0.9.1 156--> 157 158<!-- type=global --> 159 160[`setImmediate`][] is described in the [timers][] section. 161 162## `setInterval(callback, delay[, ...args])` 163<!-- YAML 164added: v0.0.1 165--> 166 167<!-- type=global --> 168 169[`setInterval`][] is described in the [timers][] section. 170 171## `setTimeout(callback, delay[, ...args])` 172<!-- YAML 173added: v0.0.1 174--> 175 176<!-- type=global --> 177 178[`setTimeout`][] is described in the [timers][] section. 179 180## `TextDecoder` 181<!-- YAML 182added: v11.0.0 183--> 184 185<!-- type=global --> 186 187The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section. 188 189## `TextEncoder` 190<!-- YAML 191added: v11.0.0 192--> 193 194<!-- type=global --> 195 196The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section. 197 198## `URL` 199<!-- YAML 200added: v10.0.0 201--> 202 203<!-- type=global --> 204 205The WHATWG `URL` class. See the [`URL`][] section. 206 207## `URLSearchParams` 208<!-- YAML 209added: v10.0.0 210--> 211 212<!-- type=global --> 213 214The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section. 215 216## `WebAssembly` 217<!-- YAML 218added: v8.0.0 219--> 220 221<!-- type=global --> 222 223* {Object} 224 225The object that acts as the namespace for all W3C 226[WebAssembly][webassembly-org] related functionality. See the 227[Mozilla Developer Network][webassembly-mdn] for usage and compatibility. 228 229[`TextDecoder`]: util.html#util_class_util_textdecoder 230[`TextEncoder`]: util.html#util_class_util_textencoder 231[`URLSearchParams`]: url.html#url_class_urlsearchparams 232[`URL`]: url.html#url_class_url 233[`__dirname`]: modules.html#modules_dirname 234[`__filename`]: modules.html#modules_filename 235[`clearImmediate`]: timers.html#timers_clearimmediate_immediate 236[`clearInterval`]: timers.html#timers_clearinterval_timeout 237[`clearTimeout`]: timers.html#timers_cleartimeout_timeout 238[`console`]: console.html 239[`exports`]: modules.html#modules_exports 240[`module`]: modules.html#modules_module 241[`process.nextTick()`]: process.html#process_process_nexttick_callback_args 242[`process` object]: process.html#process_process 243[`require()`]: modules.html#modules_require_id 244[`setImmediate`]: timers.html#timers_setimmediate_callback_args 245[`setInterval`]: timers.html#timers_setinterval_callback_delay_args 246[`setTimeout`]: timers.html#timers_settimeout_callback_delay_args 247[buffer section]: buffer.html 248[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects 249[module system documentation]: modules.html 250[timers]: timers.html 251[webassembly-mdn]: https://developer.mozilla.org/en-US/docs/WebAssembly 252[webassembly-org]: https://webassembly.org 253