In this article, Mytour will introduce you to the Global Objects present in Node.js modules. With these Global Objects, we can directly call or use them. Global objects can be modules, functions, strings, or an object.
Exploring the Global Object in Node.js
Global Object in Node.js
Global objects in Node.js are inherently global and available in all modules. As mentioned by Mytour, global objects can be directly called or used, and they can be modules, functions, strings, or an object.
Global String
__filename: represents the filename of the currently executing code. It is simply the absolute path to the file containing this code segment. When working with a main program, it avoids conflicting with the filename used in the command line. The path to the module file is a value inside the module as shown below:
Upon execution of the code snippet, the absolute file path is displayed in italics on the console interface as follows:
C:odeskAbhishek ThakurNodeJSGlobalObjectshello-world-server.js
__dirname: Represents the name of the directory existing within the executing script. When the value of this Global string is captured, it prints the absolute directory path at the location of the script file on the computer's hard drive as depicted below:
Upon executing the above code snippet, the absolute file path is displayed in italics on the console interface as shown below:
C:odeskAbhishek ThakurNodeJSGlobalObjects
Global Function
- setTimeout (callback, milliseconds): The setTimeout function (callback, milliseconds) is a Global function used to execute a callback after a specified waiting time in milliseconds.
These are the two parameters of the function. The delay time depends on various factors such as the operating system configuration and system load time. The maximum time a timer can last is 24.8 days.
Below is an example of a timer function calling back a function and printing a string within that function after 3000 ms (i.e., 3 seconds).
Upon executing the above code snippet, the string is printed on the console after a 3-second delay in the following format:
Hello, welcome to the World of global objects in Node.js!
- clearTimeout (time): This is a Global function used to stop a previously created timer set by the setTimeout() function. The time parameter is returned by the setTimeout() function.
Here's an example of the clearTimeout function clearing the timer set by the setTimeout function.
Upon execution of the above code snippet, nothing is printed on the console after a 3-second delay because the setTimeout timer is canceled by the clearTimeout function.
- setInterval (callback, milliseconds): The Global setInterval function (callback, milliseconds) is used to run a callback after a specified time interval in milliseconds.
The delay time depends on various factors such as the operating system configuration and system load time. The maximum time a timer can last is 24.8 days.
Here's an example of the setInterval function, calling a function and printing a string within that function once after 3000 ms (i.e., 3 seconds). Additionally, it returns a timer value. To clear this timer, you can use the clearInterval function (timer).
Upon execution of the above code snippet, the string is printed on the console every 3 seconds in the following format:
Global Object
The table below lists the common Global objects frequently used in applications:
1. Console
In Node.js, the console is a Global object used to print various message levels to stdout and stderr. Below are the methods used to print information, warnings, and error messages:
Below is an example of using console:
console.log( __dirname );
console.info( __filename );
2. Process
The Process object in Node.js is utilized to retrieve information about the current process, providing various events and methods related to process activities.
Utilizing these methods helps you better control system interactions. Below is a table listing common processes used in applications:
So, the article above has just introduced you to what the Global object is in Node.js, right? Additionally, readers can refer to some other articles on Mytour to delve deeper into Event, File System (FS) in Node.js.
Next up, Mytour will further introduce you to the Utility Module in Node.js.
