Node.js Tutorial - Node.js global

The variable global is our handle to the global namespace in Node.js.

All the true globals we have seen, such as console, setTimeout, and process, are members of the global variable.

We can even add members to the global variable to make it available everywhere.


The following code checks the true global functions.

console.log(console === global.console); // true 
console.log(setTimeout === global.setTimeout); // true 
console.log(process === global.process); // true 


The following code shows how to use global variables across javascript file.


global.something = 123; 


// Add something to global 
console.log(something); // 123 

__filename and __dirname

These variables are available in each file and give you the full path to the file and directory for the current module.

They include everything right up to the root of the current drive this file resides on.

The following code shows values.


The code above generates the following result.