Home >Web Front-end >JS Tutorial >What are the global variables in nodejs?
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.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
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 );
Execute the main.js file, the code is as follows:
$ node main.js /web/com/runoob/nodejs/main.js
__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 );
Execute the main.js file, the code is as follows:
$ node main.js /web/com/runoob/nodejs
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.
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;
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);};
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!