Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js global object


May 10, 2021 Node.js


Table of contents


The global object

In the Node .js we can access the global object directly.

These objects are available in all modules, some not in the global scope but in the module scope, as described in this article.

global

  • The global namespace object.

In a browser, the global scope is the top-level domain. I f you define the variable var something within var something it will be the global variable. In Node, the top-level domain is not var something a global domain;

process

  • {Object}

The process object. See the process object section.

console

  • {Object}

Used to print stdout and stderr. See console chapter.

Class: Buffer

  • {Function}

Used to process binary data. See the buffer section.

require()

  • {Function}

Introduce modules. S ee modules section. require not actually global, but is valid for each local module.

require.resolve()

Use the require() mechanism to find the mode location, but do not load the module, only return the resolved file name.

require.cache

  • {Object}

This object is cached when the module is introduced. By removing the object key value, the next call require overloads the module.

require.extensions

稳定性: 0 - 抛弃
  • {Object}

Instructs require on how to handle specific file extensions.

Treat .sjs file .js file:

require.extensions['.sjs'] = require.extensions['.js'];

Discard the previous list used to load on-demand non-JavaScript modules to node. In fact, there are better ways to solve this problem, such as loading modules with other node programs or compiling JavaScript in advance.

This function may never be removed because the module system is locked. Changing it can create a bug, so it's best not to move it.

__filename

  • {String}

The file name of the code being executed is the relative path. F or the main program, this does not necessarily use the same file name as on the command line. The value in the module is the path to the module file.

Columns such as the /Users/mjr node example.js

console.log(__filename);
// /Users/mjr/example.js

__filename not global, but module-local.

__dirname

  • {String}

The name of the folder in which the script code is executed.

Columns such as the /Users/mjr node example.js

console.log(__dirname);
// /Users/mjr

__dirname not global, but module-local.

module

  • {Object}

The reference to the current module. With require() module.exports module output is available.

module is not global, but the module is local.

For more information, see module system documentation.

exports

module.exports For information on when exports module.exports you can refer to the module system documentation.

module is not global, but the module is local.

For more information, see module system documentation.

See module for more information.

setTimeout(cb, ms)

The callback ms called back at least after ms milliseconds. Actual latency depends on external factors, such as the granularity and load of the operating system.

The timeout values have a valid range of 1-2,147,483,647. I f the range is exceeded, it will change to 1 millisecond. In general, the timer should not exceed 24.8 days.

Returns a handle value that represents the timer.

clearTimeout(t)

Stop a timer that setTimeout() T he callback will no longer be executed.

setInterval(cb, ms)

The ms function cb is called every ms cb T he actual interval depends on external factors, such as the granularity of the operating system and the system load. Usually greater ms

The effective range of the interval values is 1-2,147,483,647. I f the range is exceeded, it will change to 1 millisecond. In general, the timer should not exceed 24.8 days.

Returns a handle value that represents the timer.

clearInterval(t)

Stop a timer that setInterval() T he callback will no longer be executed.

The timer function is a global variable. See the timers section.