Home > Web Front-end > JS Tutorial > body text

What are the global variables in nodejs?

青灯夜游
Release: 2021-11-23 17:46:42
Original
3592 people have browsed it

In nodejs, there are two global variables: 1. "__filename", which represents the file name of the script currently being executed, and can output the absolute path of the file location; 2. "__dirname", which represents the current execution The directory where the script is located.

What are the global variables in nodejs?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Global variables:

1) _filename: Points to the name of the currently running script file.

2) _dirname: Points to the directory where the currently running script is located.

__filename

__filename represents the file name of the currently executing script. It will output the absolute path of the file location, and it may not be the same as the file name specified by the command line parameter. If in a module, the returned value is the path to the module file.

Example

Create the file main.js, the code is as follows:

// 输出全局变量 
__filename 的值console.log( __filename );
Copy after login

Execute the main.js file, the code is as follows:

$ node main.js
/web/com/runoob/nodejs/main.js
Copy after login

__dirname

__dirname indicates the directory where the currently executing script is located.

Example

Create the file main.js, the code is as follows:

// 输出全局变量
__dirname 的值console.log( __dirname );
Copy after login

Execute the main.js file, the code is as follows:

$ node main.js
/web/com/runoob/nodejs
Copy after login

Global objects and global variables:

The most fundamental role of global is to serve as the host of global variables. According to the definition of ECMAScript, variables that meet the following conditions are global variables:

Variables defined in the outermost layer; attributes of global objects; implicitly defined variables (variables that are not directly assigned a value).

When you define a global variable, the variable will also become a property of the global object, and vice versa. It should be noted that in Node.js you cannot define variables at the outermost level, because all user code belongs to the current module, and the module itself is not the outermost context.

Quasi-global variables

Local variables inside the module, the objects they point to vary depending on the module, but they are applicable to all modules and can be regarded as pseudo-global variables. Mainly module, module.exports, exports, etc.

The module variable refers to the current module. The module.exports variable represents the external output interface of the current module. When other files load the module, they actually read the module.exports variable.

  • module.id The identifier of the module, usually the file name of the module.

  • module.filename The file name of the module.

  • module.loaded returns a Boolean value indicating whether the module has completed loading.

  • module.parent Returns the module that uses this module.

  • module.children returns an array representing other modules to be used by this module.

What needs to be pointed out here is that the exports variable is actually a link to the module.exports object, which is equivalent to a line of such a command at the head of each module.

var exports = module.exports;
Copy after login

The result of this is that when exporting the module interface to the outside world, you can add methods to the exports object, but you cannot directly point the exports variable to a function:

exports.自定义模块 = function (x){ console.log(x);};
Copy after login

The above writing method is invalid , because it cuts off the link between exports and module.exports. However, it is okay to write like this.

For more node-related knowledge, please visit: nodejs tutorial! !

The above is the detailed content of What are the global variables in nodejs?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template