• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: `AbortController`
21<!-- YAML
22added: v14.17.0
23-->
24
25> Stability: 1 - Experimental
26
27<!-- type=global -->
28
29A utility class used to signal cancelation in selected `Promise`-based APIs.
30The API is based on the Web API [`AbortController`][].
31
32To use, launch Node.js using the `--experimental-abortcontroller` flag.
33
34```js
35const ac = new AbortController();
36
37ac.signal.addEventListener('abort', () => console.log('Aborted!'),
38                           { once: true });
39
40ac.abort();
41
42console.log(ac.signal.aborted);  // Prints True
43```
44
45### `abortController.abort()`
46<!-- YAML
47added: v14.17.0
48-->
49
50Triggers the abort signal, causing the `abortController.signal` to emit
51the `'abort'` event.
52
53### `abortController.signal`
54<!-- YAML
55added: v14.17.0
56-->
57
58* Type: {AbortSignal}
59
60### Class: `AbortSignal`
61<!-- YAML
62added: v14.17.0
63-->
64
65* Extends: {EventTarget}
66
67The `AbortSignal` is used to notify observers when the
68`abortController.abort()` method is called.
69
70#### Static method: `AbortSignal.abort()`
71<!-- YAML
72added: v14.17.0
73-->
74
75* Returns: {AbortSignal}
76
77Returns a new already aborted `AbortSignal`.
78
79#### Event: `'abort'`
80<!-- YAML
81added: v14.17.0
82-->
83
84The `'abort'` event is emitted when the `abortController.abort()` method
85is called. The callback is invoked with a single object argument with a
86single `type` property set to `'abort'`:
87
88```js
89const ac = new AbortController();
90
91// Use either the onabort property...
92ac.signal.onabort = () => console.log('aborted!');
93
94// Or the EventTarget API...
95ac.signal.addEventListener('abort', (event) => {
96  console.log(event.type);  // Prints 'abort'
97}, { once: true });
98
99ac.abort();
100```
101
102The `AbortController` with which the `AbortSignal` is associated will only
103ever trigger the `'abort'` event once. We recommended that code check
104that the `abortSignal.aborted` attribute is `false` before adding an `'abort'`
105event listener.
106
107Any event listeners attached to the `AbortSignal` should use the
108`{ once: true }` option (or, if using the `EventEmitter` APIs to attach a
109listener, use the `once()` method) to ensure that the event listener is
110removed as soon as the `'abort'` event is handled. Failure to do so may
111result in memory leaks.
112
113#### `abortSignal.aborted`
114<!-- YAML
115added: v14.17.0
116-->
117
118* Type: {boolean} True after the `AbortController` has been aborted.
119
120#### `abortSignal.onabort`
121<!-- YAML
122added: v14.17.0
123-->
124
125* Type: {Function}
126
127An optional callback function that may be set by user code to be notified
128when the `abortController.abort()` function has been called.
129
130## Class: `Buffer`
131<!-- YAML
132added: v0.1.103
133-->
134
135<!-- type=global -->
136
137* {Function}
138
139Used to handle binary data. See the [buffer section][].
140
141## `__dirname`
142
143This variable may appear to be global but is not. See [`__dirname`][].
144
145## `__filename`
146
147This variable may appear to be global but is not. See [`__filename`][].
148
149## `clearImmediate(immediateObject)`
150<!-- YAML
151added: v0.9.1
152-->
153
154<!--type=global-->
155
156[`clearImmediate`][] is described in the [timers][] section.
157
158## `clearInterval(intervalObject)`
159<!-- YAML
160added: v0.0.1
161-->
162
163<!--type=global-->
164
165[`clearInterval`][] is described in the [timers][] section.
166
167## `clearTimeout(timeoutObject)`
168<!-- YAML
169added: v0.0.1
170-->
171
172<!--type=global-->
173
174[`clearTimeout`][] is described in the [timers][] section.
175
176## `console`
177<!-- YAML
178added: v0.1.100
179-->
180
181<!-- type=global -->
182
183* {Object}
184
185Used to print to stdout and stderr. See the [`console`][] section.
186
187## `exports`
188
189This variable may appear to be global but is not. See [`exports`][].
190
191## `global`
192<!-- YAML
193added: v0.1.27
194-->
195
196<!-- type=global -->
197
198* {Object} The global namespace object.
199
200In browsers, the top-level scope is the global scope. This means that
201within the browser `var something` will define a new global variable. In
202Node.js this is different. The top-level scope is not the global scope;
203`var something` inside a Node.js module will be local to that module.
204
205## `module`
206
207This variable may appear to be global but is not. See [`module`][].
208
209## `process`
210<!-- YAML
211added: v0.1.7
212-->
213
214<!-- type=global -->
215
216* {Object}
217
218The process object. See the [`process` object][] section.
219
220## `queueMicrotask(callback)`
221<!-- YAML
222added: v11.0.0
223-->
224
225<!-- type=global -->
226
227* `callback` {Function} Function to be queued.
228
229The `queueMicrotask()` method queues a microtask to invoke `callback`. If
230`callback` throws an exception, the [`process` object][] `'uncaughtException'`
231event will be emitted.
232
233The microtask queue is managed by V8 and may be used in a similar manner to
234the [`process.nextTick()`][] queue, which is managed by Node.js. The
235`process.nextTick()` queue is always processed before the microtask queue
236within each turn of the Node.js event loop.
237
238```js
239// Here, `queueMicrotask()` is used to ensure the 'load' event is always
240// emitted asynchronously, and therefore consistently. Using
241// `process.nextTick()` here would result in the 'load' event always emitting
242// before any other promise jobs.
243
244DataHandler.prototype.load = async function load(key) {
245  const hit = this._cache.get(key);
246  if (hit !== undefined) {
247    queueMicrotask(() => {
248      this.emit('load', hit);
249    });
250    return;
251  }
252
253  const data = await fetchData(key);
254  this._cache.set(key, data);
255  this.emit('load', data);
256};
257```
258
259## `require()`
260
261This variable may appear to be global but is not. See [`require()`][].
262
263## `setImmediate(callback[, ...args])`
264<!-- YAML
265added: v0.9.1
266-->
267
268<!-- type=global -->
269
270[`setImmediate`][] is described in the [timers][] section.
271
272## `setInterval(callback, delay[, ...args])`
273<!-- YAML
274added: v0.0.1
275-->
276
277<!-- type=global -->
278
279[`setInterval`][] is described in the [timers][] section.
280
281## `setTimeout(callback, delay[, ...args])`
282<!-- YAML
283added: v0.0.1
284-->
285
286<!-- type=global -->
287
288[`setTimeout`][] is described in the [timers][] section.
289
290## `TextDecoder`
291<!-- YAML
292added: v11.0.0
293-->
294
295<!-- type=global -->
296
297The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section.
298
299## `TextEncoder`
300<!-- YAML
301added: v11.0.0
302-->
303
304<!-- type=global -->
305
306The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section.
307
308## `URL`
309<!-- YAML
310added: v10.0.0
311-->
312
313<!-- type=global -->
314
315The WHATWG `URL` class. See the [`URL`][] section.
316
317## `URLSearchParams`
318<!-- YAML
319added: v10.0.0
320-->
321
322<!-- type=global -->
323
324The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section.
325
326## `WebAssembly`
327<!-- YAML
328added: v8.0.0
329-->
330
331<!-- type=global -->
332
333* {Object}
334
335The object that acts as the namespace for all W3C
336[WebAssembly][webassembly-org] related functionality. See the
337[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
338
339[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
340[`TextDecoder`]: util.md#util_class_util_textdecoder
341[`TextEncoder`]: util.md#util_class_util_textencoder
342[`URLSearchParams`]: url.md#url_class_urlsearchparams
343[`URL`]: url.md#url_class_url
344[`__dirname`]: modules.md#modules_dirname
345[`__filename`]: modules.md#modules_filename
346[`clearImmediate`]: timers.md#timers_clearimmediate_immediate
347[`clearInterval`]: timers.md#timers_clearinterval_timeout
348[`clearTimeout`]: timers.md#timers_cleartimeout_timeout
349[`console`]: console.md
350[`exports`]: modules.md#modules_exports
351[`module`]: modules.md#modules_module
352[`process.nextTick()`]: process.md#process_process_nexttick_callback_args
353[`process` object]: process.md#process_process
354[`require()`]: modules.md#modules_require_id
355[`setImmediate`]: timers.md#timers_setimmediate_callback_args
356[`setInterval`]: timers.md#timers_setinterval_callback_delay_args
357[`setTimeout`]: timers.md#timers_settimeout_callback_delay_args
358[buffer section]: buffer.md
359[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
360[module system documentation]: modules.md
361[timers]: timers.md
362[webassembly-mdn]: https://developer.mozilla.org/en-US/docs/WebAssembly
363[webassembly-org]: https://webassembly.org
364