Node.js Global Object
Node.js Tutorial » Node.js Global Object
What is the global object in nodeJS?
Node.js global objects are global in nature and are available in all modules.
We do not need to include these objects in our application, rather we can use them directly.
These objects are modules such as: arrays, functions, strings and object itself as explained below..
In this chapter we study the following objects:
__filename
__dirname
setTimeout(cb, ms)
clearTimeout(t)
setInterval(cb, ms)
Node.js __filename object
__filename indicates the name of the running script file.
The absolute path to the location of the output file, and the command line parameter and specify the file name could be different.
If it is in the module, the returned value is the path to the module file..
Create a js file named my_test_main.js with the following code −
console.log( __filename );
Output:
$node main.js
/home/files_script/my_test_main.js
Related subjects:
node.js http module
node.js modules
Install nodejs
Tags: nodejs global: variable across file, object, variable, error handler, function, typescript, scope, exception handler, await, node js globalthis
nodejs global object documentation, set global object, request object
What is nodeJS global?
How do I declare global in node JS?
Which of the following are global objects in Node JS?
What is the global object in nodeJS?
What is a global object?
What is __ Dirname global object in nodeJS?
How do I declare a global variable in node JS?
Node js __dirname object
Node js __dirname object represents the name of the directory where currently executing script resides in.
Create a js file named my_test_main.js with the following code:
console.log( __dirname );
Output:
$node main.js
/home/files_script
Nodejs setTimeout(cb, ms)
The setTimeout(cb, ms) global function is used to cb (run callback) after the least ms (millisecond).
The delay may depend on like OS timer granularity and system load.
SetTimeout () function is specified only once and
It returns a handle value represents the timer.
console.log( "Hi, This is my script node js");
}
// Now call above function after 4 seconds
setTimeout(printMyscript, 4000);
Output:
$node main.js
Hi, This is my script node js
clearTimeout (t)
clearTimeout (t) is used to stop a global function before passing setTimeout () to create a timer.
console.log( "Hi, This is my script node js");
}
// Now call above function after 4 seconds
var t = setTimeout(printMyscript, 4000);
// Now clear the timer
clearTimeout(t);
setInterval(cb, ms)
The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds.
console.log( "Hi, This is my script node js");
}
// Now call above function after 4 seconds
setInterval(printMyscript, 4000);
nodejs global: variable across file, object, variable, error handler, function, typescript, scope, exception handler, await, node js globalthis
nodejs global object documentation, set global object, request object What is nodeJS global?
How do I declare global in node JS?
Which of the following are global objects in Node JS?
What is the global object in nodeJS?
What is a global object?
What is __ Dirname global object in nodeJS?
How do I declare a global variable in node JS?
Node.js Global Object - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.