2012-02-27 20:09:33 +01:00
|
|
|
# Global Objects
|
|
|
|
|
|
|
|
<!-- type=misc -->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
These objects are available in all modules. Some of these objects aren't
|
2011-04-12 01:48:18 +02:00
|
|
|
actually in the global scope but in the module scope - this will be noted.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-20 19:40:17 +02:00
|
|
|
The objects listed here are specific to Node.js. There are a number of
|
|
|
|
[built-in objects][] that are part of the JavaScript language itself, which are
|
|
|
|
also globally accessible.
|
|
|
|
|
2012-03-04 08:38:52 +01:00
|
|
|
## Class: Buffer
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.103
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=global -->
|
|
|
|
|
2012-03-04 08:38:52 +01:00
|
|
|
* {Function}
|
2011-07-15 22:11:14 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Used to handle binary data. See the [buffer section][].
|
2011-04-19 01:52:53 +02:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
## \_\_dirname
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.27
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=var -->
|
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
* {String}
|
2012-04-17 07:15:51 +02:00
|
|
|
|
2016-12-29 23:11:02 +01:00
|
|
|
The directory name of the current module. This the same as the
|
|
|
|
[`path.dirname()`][] of the [`__filename`][].
|
|
|
|
|
|
|
|
`__dirname` isn't actually a global but rather local to each module.
|
2012-04-17 07:15:51 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
Example: running `node example.js` from `/Users/mjr`
|
2012-04-17 07:15:51 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
console.log(__dirname);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: /Users/mjr
|
2016-12-29 23:11:02 +01:00
|
|
|
console.log(path.dirname(__filename));
|
|
|
|
// Prints: /Users/mjr
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-04-29 07:10:24 +02:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
## \_\_filename
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=var -->
|
2011-06-03 08:14:35 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
* {String}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-12-29 23:11:02 +01:00
|
|
|
The file name of the current module. This is the resolved absolute path of the
|
|
|
|
current module file.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-12-29 23:11:02 +01:00
|
|
|
For a main program this is not necessarily the same as the file name used in the
|
|
|
|
command line.
|
|
|
|
|
|
|
|
See [`__dirname`][] for the directory name of the current module.
|
|
|
|
|
|
|
|
`__filename` isn't actually a global but rather local to each module.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
Running `node example.js` from `/Users/mjr`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
console.log(__filename);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: /Users/mjr/example.js
|
2016-12-29 23:11:02 +01:00
|
|
|
console.log(__dirname);
|
|
|
|
// Prints: /Users/mjr
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-12-29 23:11:02 +01:00
|
|
|
Given two modules: `a` and `b`, where `b` is a dependency of
|
|
|
|
`a` and there is a directory structure of:
|
|
|
|
|
|
|
|
* `/Users/mjr/app/a.js`
|
|
|
|
* `/Users/mjr/app/node_modules/b/b.js`
|
|
|
|
|
|
|
|
References to `__filename` within `b.js` will return
|
|
|
|
`/Users/mjr/app/node_modules/b/b.js` while references to `__filename` within
|
|
|
|
`a.js` will return `/Users/mjr/app/a.js`.
|
2011-04-12 01:48:18 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
## clearImmediate(immediateObject)
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
<!--type=global-->
|
|
|
|
|
|
|
|
[`clearImmediate`] is described in the [timers][] section.
|
|
|
|
|
|
|
|
## clearInterval(intervalObject)
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
<!--type=global-->
|
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
[`clearInterval`] is described in the [timers][] section.
|
2015-11-04 18:12:06 +01:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
## clearTimeout(timeoutObject)
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2016-03-22 00:03:41 +01:00
|
|
|
|
|
|
|
<!--type=global-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
[`clearTimeout`] is described in the [timers][] section.
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
## console
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.100
|
|
|
|
-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
<!-- type=global -->
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Used to print to stdout and stderr. See the [`console`][] section.
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
## exports
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.12
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=var -->
|
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
A reference to the `module.exports` that is shorter to type.
|
|
|
|
See [module system documentation][] for details on when to use `exports` and
|
|
|
|
when to use `module.exports`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
`exports` isn't actually a global but rather local to each module.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
See the [module system documentation][] for more information.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
## global
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.27
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
<!-- type=global -->
|
|
|
|
|
|
|
|
* {Object} The global namespace object.
|
2011-04-12 01:48:18 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
In browsers, the top-level scope is the global scope. That means that in
|
|
|
|
browsers if you're in the global scope `var something` will define a global
|
|
|
|
variable. In Node.js this is different. The top-level scope is not the global
|
|
|
|
scope; `var something` inside an Node.js module will be local to that module.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## module
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.16
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=var -->
|
|
|
|
|
|
|
|
* {Object}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-23 20:14:20 +01:00
|
|
|
A reference to the current module. In particular
|
2013-10-26 07:03:02 +02:00
|
|
|
`module.exports` is used for defining what a module exports and makes
|
|
|
|
available through `require()`.
|
|
|
|
|
2011-04-14 18:54:36 +02:00
|
|
|
`module` isn't actually a global but rather local to each module.
|
2011-04-01 18:19:00 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
See the [module system documentation][] for more information.
|
2012-02-23 09:18:17 +01:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
## process
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.7
|
|
|
|
-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
<!-- type=global -->
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The process object. See the [`process` object][] section.
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
## require()
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.13
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
<!-- type=var -->
|
2011-05-19 21:49:29 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
* {Function}
|
2013-10-26 07:03:02 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
To require modules. See the [Modules][] section. `require` isn't actually a
|
|
|
|
global but rather local to each module.
|
2011-05-19 21:49:29 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
### require.cache
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2012-02-23 09:18:17 +01:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
* {Object}
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
Modules are cached in this object when they are required. By deleting a key
|
2016-04-12 20:10:49 +02:00
|
|
|
value from this object, the next `require` will reload the module. Note that
|
|
|
|
this does not apply to [native addons][], for which reloading will result in an
|
|
|
|
Error.
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
### require.extensions
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
deprecated: v0.10.6
|
|
|
|
-->
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 0 - Deprecated
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
* {Object}
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
Instruct `require` on how to handle certain file extensions.
|
|
|
|
|
|
|
|
Process files with the extension `.sjs` as `.js`:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
require.extensions['.sjs'] = require.extensions['.js'];
|
|
|
|
```
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
**Deprecated** In the past, this list has been used to load
|
|
|
|
non-JavaScript modules into Node.js by compiling them on-demand.
|
|
|
|
However, in practice, there are much better ways to do this, such as
|
|
|
|
loading modules via some other Node.js program, or compiling them to
|
|
|
|
JavaScript ahead of time.
|
|
|
|
|
2016-10-19 15:38:30 +02:00
|
|
|
Since the module system is locked, this feature will probably never go
|
2015-11-04 18:12:06 +01:00
|
|
|
away. However, it may have subtle bugs and complexities that are best
|
|
|
|
left untouched.
|
|
|
|
|
2016-10-19 15:38:30 +02:00
|
|
|
Note that the number of file system operations that the module system
|
|
|
|
has to perform in order to resolve a `require(...)` statement to a
|
|
|
|
filename scales linearly with the number of registered extensions.
|
|
|
|
|
|
|
|
In other words, adding extensions slows down the module loader and
|
|
|
|
should be discouraged.
|
|
|
|
|
2015-11-04 18:12:06 +01:00
|
|
|
### require.resolve()
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2015-11-04 18:12:06 +01:00
|
|
|
|
|
|
|
Use the internal `require()` machinery to look up the location of a module,
|
|
|
|
but rather than loading the module, just return the resolved filename.
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-08-30 07:35:03 +02:00
|
|
|
## setImmediate(callback[, ...args])
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
<!-- type=global -->
|
|
|
|
|
|
|
|
[`setImmediate`] is described in the [timers][] section.
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-08-30 07:35:03 +02:00
|
|
|
## setInterval(callback, delay[, ...args])
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
<!-- type=global -->
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
[`setInterval`] is described in the [timers][] section.
|
2012-07-01 20:09:55 +02:00
|
|
|
|
2016-08-30 07:35:03 +02:00
|
|
|
## setTimeout(callback, delay[, ...args])
|
2016-10-03 10:49:29 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2011-04-01 18:19:00 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
<!-- type=global -->
|
2012-06-06 21:05:18 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
[`setTimeout`] is described in the [timers][] section.
|
2015-11-14 04:21:49 +01:00
|
|
|
|
2016-12-29 23:11:02 +01:00
|
|
|
[`__dirname`]: #globals_dirname
|
|
|
|
[`__filename`]: #globals_filename
|
2015-11-28 00:30:32 +01:00
|
|
|
[`console`]: console.html
|
2016-12-29 23:11:02 +01:00
|
|
|
[`path.dirname()`]: path.html#path_path_dirname_path
|
2015-11-28 00:30:32 +01:00
|
|
|
[`process` object]: process.html#process_process
|
2015-11-14 04:21:49 +01:00
|
|
|
[buffer section]: buffer.html
|
|
|
|
[module system documentation]: modules.html
|
|
|
|
[Modules]: modules.html#modules_modules
|
2016-04-12 20:10:49 +02:00
|
|
|
[native addons]: addons.html
|
2015-11-14 04:21:49 +01:00
|
|
|
[timers]: timers.html
|
2016-07-09 07:13:09 +02:00
|
|
|
[`clearImmediate`]: timers.html#timers_clearimmediate_immediate
|
|
|
|
[`clearInterval`]: timers.html#timers_clearinterval_timeout
|
|
|
|
[`clearTimeout`]: timers.html#timers_cleartimeout_timeout
|
2016-08-30 07:35:03 +02:00
|
|
|
[`setImmediate`]: timers.html#timers_setimmediate_callback_args
|
|
|
|
[`setInterval`]: timers.html#timers_setinterval_callback_delay_args
|
|
|
|
[`setTimeout`]: timers.html#timers_settimeout_callback_delay_args
|
2016-05-20 19:40:17 +02:00
|
|
|
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|