mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
5.2 KiB
212 lines
5.2 KiB
13 years ago
|
# Global Objects
|
||
|
|
||
|
<!-- type=misc -->
|
||
15 years ago
|
|
||
14 years ago
|
These objects are available in all modules. Some of these objects aren't
|
||
14 years ago
|
actually in the global scope but in the module scope - this will be noted.
|
||
15 years ago
|
|
||
13 years ago
|
## Class: Buffer
|
||
13 years ago
|
|
||
|
<!-- type=global -->
|
||
|
|
||
13 years ago
|
* {Function}
|
||
14 years ago
|
|
||
9 years ago
|
Used to handle binary data. See the [buffer section][].
|
||
14 years ago
|
|
||
9 years ago
|
## \_\_dirname
|
||
13 years ago
|
|
||
|
<!-- type=var -->
|
||
|
|
||
9 years ago
|
* {String}
|
||
13 years ago
|
|
||
9 years ago
|
The name of the directory that the currently executing script resides in.
|
||
13 years ago
|
|
||
9 years ago
|
Example: running `node example.js` from `/Users/mjr`
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
console.log(__dirname);
|
||
|
// /Users/mjr
|
||
|
```
|
||
12 years ago
|
|
||
9 years ago
|
`__dirname` isn't actually a global but rather local to each module.
|
||
12 years ago
|
|
||
9 years ago
|
For instance, 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`
|
||
|
|
||
9 years ago
|
References to `__dirname` within `b.js` will return
|
||
9 years ago
|
`/Users/mjr/app/node_modules/b` while references to `__dirname` within `a.js`
|
||
|
will return `/Users/mj/app`.
|
||
|
|
||
9 years ago
|
## \_\_filename
|
||
13 years ago
|
|
||
|
<!-- type=var -->
|
||
14 years ago
|
|
||
13 years ago
|
* {String}
|
||
15 years ago
|
|
||
14 years ago
|
The filename of the code being executed. This is the resolved absolute path
|
||
|
of this code file. For a main program this is not necessarily the same
|
||
|
filename used in the command line. The value inside a module is the path
|
||
|
to that module file.
|
||
15 years ago
|
|
||
10 years ago
|
Example: running `node example.js` from `/Users/mjr`
|
||
15 years ago
|
|
||
9 years ago
|
```js
|
||
|
console.log(__filename);
|
||
|
// /Users/mjr/example.js
|
||
|
```
|
||
15 years ago
|
|
||
14 years ago
|
`__filename` isn't actually a global but rather local to each module.
|
||
|
|
||
9 years ago
|
## clearImmediate(immediateObject)
|
||
9 years ago
|
|
||
9 years ago
|
<!--type=global-->
|
||
|
|
||
|
[`clearImmediate`] is described in the [timers][] section.
|
||
|
|
||
|
## clearInterval(intervalObject)
|
||
9 years ago
|
|
||
|
<!--type=global-->
|
||
|
|
||
9 years ago
|
[`clearInterval`] is described in the [timers][] section.
|
||
9 years ago
|
|
||
9 years ago
|
## clearTimeout(timeoutObject)
|
||
|
|
||
|
<!--type=global-->
|
||
9 years ago
|
|
||
9 years ago
|
[`clearTimeout`] is described in the [timers][] section.
|
||
9 years ago
|
|
||
|
## console
|
||
|
|
||
|
<!-- type=global -->
|
||
|
|
||
|
* {Object}
|
||
|
|
||
9 years ago
|
Used to print to stdout and stderr. See the [`console`][] section.
|
||
9 years ago
|
|
||
|
## exports
|
||
13 years ago
|
|
||
|
<!-- type=var -->
|
||
|
|
||
9 years ago
|
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`.
|
||
15 years ago
|
|
||
9 years ago
|
`exports` isn't actually a global but rather local to each module.
|
||
15 years ago
|
|
||
9 years ago
|
See the [module system documentation][] for more information.
|
||
15 years ago
|
|
||
9 years ago
|
## global
|
||
15 years ago
|
|
||
9 years ago
|
<!-- type=global -->
|
||
|
|
||
|
* {Object} The global namespace object.
|
||
14 years ago
|
|
||
9 years ago
|
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.
|
||
15 years ago
|
|
||
13 years ago
|
## module
|
||
|
|
||
|
<!-- type=var -->
|
||
|
|
||
|
* {Object}
|
||
15 years ago
|
|
||
14 years ago
|
A reference to the current module. In particular
|
||
11 years ago
|
`module.exports` is used for defining what a module exports and makes
|
||
|
available through `require()`.
|
||
|
|
||
14 years ago
|
`module` isn't actually a global but rather local to each module.
|
||
14 years ago
|
|
||
13 years ago
|
See the [module system documentation][] for more information.
|
||
13 years ago
|
|
||
9 years ago
|
## process
|
||
|
|
||
|
<!-- type=global -->
|
||
|
|
||
|
* {Object}
|
||
|
|
||
9 years ago
|
The process object. See the [`process` object][] section.
|
||
9 years ago
|
|
||
|
## require()
|
||
13 years ago
|
|
||
|
<!-- type=var -->
|
||
14 years ago
|
|
||
9 years ago
|
* {Function}
|
||
11 years ago
|
|
||
9 years ago
|
To require modules. See the [Modules][] section. `require` isn't actually a
|
||
|
global but rather local to each module.
|
||
14 years ago
|
|
||
9 years ago
|
### require.cache
|
||
13 years ago
|
|
||
9 years ago
|
* {Object}
|
||
13 years ago
|
|
||
9 years ago
|
Modules are cached in this object when they are required. By deleting a key
|
||
9 years ago
|
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.
|
||
13 years ago
|
|
||
9 years ago
|
### require.extensions
|
||
13 years ago
|
|
||
9 years ago
|
Stability: 0 - Deprecated
|
||
13 years ago
|
|
||
9 years ago
|
* {Object}
|
||
13 years ago
|
|
||
9 years ago
|
Instruct `require` on how to handle certain file extensions.
|
||
|
|
||
|
Process files with the extension `.sjs` as `.js`:
|
||
|
|
||
9 years ago
|
```js
|
||
|
require.extensions['.sjs'] = require.extensions['.js'];
|
||
|
```
|
||
9 years ago
|
|
||
|
**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.
|
||
|
|
||
|
Since the Module system is locked, this feature will probably never go
|
||
|
away. However, it may have subtle bugs and complexities that are best
|
||
|
left untouched.
|
||
|
|
||
|
### require.resolve()
|
||
|
|
||
|
Use the internal `require()` machinery to look up the location of a module,
|
||
|
but rather than loading the module, just return the resolved filename.
|
||
13 years ago
|
|
||
9 years ago
|
## setImmediate(callback[, arg][, ...])
|
||
13 years ago
|
|
||
9 years ago
|
<!-- type=global -->
|
||
|
|
||
|
[`setImmediate`] is described in the [timers][] section.
|
||
13 years ago
|
|
||
9 years ago
|
## setInterval(callback, delay[, arg][, ...])
|
||
13 years ago
|
|
||
9 years ago
|
<!-- type=global -->
|
||
13 years ago
|
|
||
9 years ago
|
[`setInterval`] is described in the [timers][] section.
|
||
13 years ago
|
|
||
9 years ago
|
## setTimeout(callback, delay[, arg][, ...])
|
||
14 years ago
|
|
||
9 years ago
|
<!-- type=global -->
|
||
13 years ago
|
|
||
9 years ago
|
[`setTimeout`] is described in the [timers][] section.
|
||
9 years ago
|
|
||
9 years ago
|
[`console`]: console.html
|
||
|
[`process` object]: process.html#process_process
|
||
9 years ago
|
[buffer section]: buffer.html
|
||
|
[module system documentation]: modules.html
|
||
|
[Modules]: modules.html#modules_modules
|
||
9 years ago
|
[native addons]: addons.html
|
||
9 years ago
|
[timers]: timers.html
|
||
9 years ago
|
[`clearImmediate`]: timers.html#timers_clearimmediate_immediateobject
|
||
|
[`clearInterval`]: timers.html#timers_clearinterval_intervalobject
|
||
|
[`clearTimeout`]: timers.html#timers_cleartimeout_timeoutobject
|
||
|
[`setImmediate`]: timers.html#timers_setimmediate_callback_arg
|
||
|
[`setInterval`]: timers.html#timers_setinterval_callback_delay_arg
|
||
|
[`setTimeout`]: timers.html#timers_settimeout_callback_delay_arg
|