Value of this in a node module:

this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node:

console.log(this);    // logs {}

module.exports.foo = 5;

console.log(this);   // log { foo:5 }

First we log an empty object because there are no values in module.exports in this module. Then we put foo on the module.exports object, when we then again log this we can see that it now logs the updated module.exports object.

How can we access the global object:

We can access the global object in node using the global keyword:

console.log(global);

The global object exposes a variety of useful properties about the environment. Also this is the place where functions as setImmediate and clearTimeout are located.

Answer from Willem van der Veen on Stack Overflow
🌐
Node.js
nodejs.org › api › globals.html
Global objects | Node.js v25.8.1 Documentation
This means that var something will define a new global variable, except within ECMAScript modules. In Node.js, this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module, regardless of whether it is a CommonJS module or an ECMAScript module.
Top answer
1 of 7
103

Value of this in a node module:

this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node:

console.log(this);    // logs {}

module.exports.foo = 5;

console.log(this);   // log { foo:5 }

First we log an empty object because there are no values in module.exports in this module. Then we put foo on the module.exports object, when we then again log this we can see that it now logs the updated module.exports object.

How can we access the global object:

We can access the global object in node using the global keyword:

console.log(global);

The global object exposes a variety of useful properties about the environment. Also this is the place where functions as setImmediate and clearTimeout are located.

2 of 7
79

While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module.

You can read more about it in the NodeJS documentation where it says:

global

<Object> The global namespace object.

In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside an Node.js module will be local to that module.

And in your code when you write:

  • console.log(this) in an empty js file(module) it will print an empty object {} referring to your empty module.
  • console.log(this); inside a self invoking function, this will point to the global nodeJS scope object which contains all NodeJS common properties and methods such as require(), module, exports, console...
  • console.log(this) with strict mode inside a self invoking function it will print undefined as a self invoked function doesn't have a default local scope object in Strict mode.
Discussions

How to deal with global variables when modularizing a node.js app?
As much as possible, anything that a function changes should be returned from it rather than modified in place. The purer a function is, the easier it is to test, to reason about, and to compose. Your second best bet if that's impractical is to turn the globals into data attached to the "this" object that your functions are being called as methods on. This still improves your ability to reason about data because it is scoped to a set of functions rather than the whole program. I would caution you against having a giant "context" object that holds all of the mutable globals that you pass around to every function. This is the easiest thing to do when refactoring from a program with lots of globals, but it doesn't actually buy you much over the globals. You're still left with a lot of implicit state that you need to set up before calling each function. More on reddit.com
🌐 r/node
4
1
May 7, 2016
What is the purpose of the global object in JavaScript?
Pretty much everything is stored in the window object. It's easier to think of the window object as the root object. Whenever you define anything it is either a part of the window object or it is part of another object who has a parent somewhere that is in the window object. More on reddit.com
🌐 r/learnjavascript
18
18
July 18, 2018
How to set and access global variables in node.js
Set and export those values from a separate file, just import them wherever needed. Imports are cached, meaning the code in that file will be executed just once. More on reddit.com
🌐 r/node
3
0
May 31, 2023
Why this is empty in global scope in Nodejs?
Hi, here it's explained: https://stackoverflow.com/questions/43627622/what-is-the-global-object-in-nodejs But in short: The 'global this' is referring to the current module, to itself, which is empty by the time it executes the console.log To access the global object in node, use the global keyword More on reddit.com
🌐 r/node
5
17
October 18, 2018
People also ask

How can the global object be used for debugging in Node.js?
The global object can be leveraged to define properties or functions that are accessible across the application, which assists with debugging. For example, custom loggers or state inspectors can be attached to global to help trace issues. This makes it possible to inspect or modify variables without importing them into every module. However, caution is necessary, as this can introduce side effects.
🌐
upgrad.com
upgrad.com › home › blog › software development › global objects in node js: how they can make or break your code?
Don't Ship Code Without Knowing Global Objects in Node js!
How do global objects in Node js contribute to error handling?
Global objects, like process, play a key role in error handling in Node.js applications. For example, process.on('uncaughtException') allows you to catch unhandled exceptions, preventing the application from crashing. By listening for specific error events, you can log or handle errors more gracefully. Additionally, process.exit() can be used to shut down the application cleanly after handling critical errors.
🌐
upgrad.com
upgrad.com › home › blog › software development › global objects in node js: how they can make or break your code?
Don't Ship Code Without Knowing Global Objects in Node js!
How are global objects in Node js different from local variables?
Global objects in Node js are accessible throughout the entire application, regardless of scope or module. In contrast, local variables are confined to the block or function in which they are defined. Global objects, like process or global, are available everywhere, making them useful for application-wide configuration management. On the other hand, local variables maintain modularity by limiting scope, which helps ensure encapsulation.
🌐
upgrad.com
upgrad.com › home › blog › software development › global objects in node js: how they can make or break your code?
Don't Ship Code Without Knowing Global Objects in Node js!
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › node-js-global-objects
NodeJS Global Objects - GeeksforGeeks
January 17, 2026 - The global object stores application-wide variables and functions. These globals can be accessed from any file in the Node.js app.
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_global_objects.htm
Node.js - Global Objects
Unlike the browser-based JavaScript, where the window object server as the global scope, Node.js uses the global as its global objects.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_object
Global object - Glossary - MDN Web Docs - Mozilla
Code running in a Worker has a WorkerGlobalScope object as its global object. Scripts running under Node.js have an object called global as their global object.
🌐
Medium
medium.com › @a.kago1988 › global-objects-variables-and-methods-in-javascript-and-node-js-f6bffc74e792
Built-In Objects, Variables, and Methods. What the Node.js Runtime Provides | by Andreas 🎧 Kagoshima | Medium
2 weeks ago - This object includes methods that ... with process. It can be found here. ... In Node.js, the global object itself is global, similar to how browsers use window....
Find elsewhere
🌐
Node.js
nodejs.org › download › release › v14.16.1 › docs › api › globals.html
Global objects | Node.js v14.16.1 Documentation
This means that within the browser var something will define a new global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module. This variable may appear to be global but is not. See module. ... The process object.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
July 1, 2022 - The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment. In a browser it is named window, for Node.js it is global, for other environments it may have ...
🌐
W3Resource
w3resource.com › node.js › nodejs-global-object.php
Node.js : Global Objects - w3resource
November 13, 2023 - Some of these objects are true globals and can be accessed from anywhere, other exist at module level in every module. ... The global namespace. Setting a property to this namespace makes it globally visible within the running process. ... In the browser, the top-level property is the global scope. Therefore within the browser, the var var_name will define a global variable. In Node var var_name inside a module is the local to that module i.e.
🌐
Upgrad
upgrad.com › home › blog › software development › global objects in node js: how they can make or break your code?
Don't Ship Code Without Knowing Global Objects in Node js!
July 7, 2025 - Global objects in Node js are built-in objects that provide direct access to system-level features and resources, essential for managing runtime and application behavior. These objects enable operations such as file handling, stream management, ...
🌐
Node.js
nodejs.org › download › release › v13.13.0 › docs › api › globals.html
Global Objects | Node.js v13.13.0 Documentation
This means that within the browser var something will define a new global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module. This variable may appear to be global but is not. See module. ... The process object.
🌐
Javatpoint
javatpoint.com › nodejs-global-objects
Node.js Global Objects
Node.js provides the facility to get process information such as process id, architecture, platform, version, release, uptime, upu usage etc. It can also be used to kill process, set uid, set groups, unmask etc. The process is a global object, an instance of EventEmitter, can be accessed...
🌐
Stack Abuse
stackabuse.com › using-global-variables-in-node-js
Using Global Variables in Node.js
July 8, 2024 - Now that we have a better understanding ... variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in....
🌐
Node.js
nodejs.org › download › release › v0.9.7 › docs › api › globals.html
Global Objects Node.js v0.9.7 Manual & Documentation
An object which is shared between all instances of the current module and made accessible through require(). exports is the same as the module.exports object. exports isn't actually a global but rather local to each module.
🌐
Greenroots
blog.greenroots.info › nodejs-knowing-the-global-objects
Node.js - Knowing the Global Objects
July 19, 2021 - By definition A Global object is, An object that always exists in the global scope. For web browsers, we have a window object. It provides the “browser window” functionality and also plays the role of a global object.
🌐
Node.js
nodejs.org › download › release › v0.9.5 › docs › api › globals.html
Global Objects Node.js v0.9.5 Manual & Documentation
March 22, 2019 - An object which is shared between all instances of the current module and made accessible through require(). exports is the same as the module.exports object. exports isn't actually a global but rather local to each module.
🌐
Pabbly
pabbly.com › home › tutorials › nodejs › node.js – global objects
Node.js Global Objects
February 9, 2019 - These objects are global in nature and are available in all modules. There is no need to include these objects in our application, rather we can use them directly. These objects are modules, functions, strings, object etc.
🌐
Contentful
contentful.com › blog › the-global-object-in-javascript
What is the global object in JavaScript? A practical guide for developers | Contentful
March 14, 2024 - Web browser Window global objects ... like displaying alert pop-ups and working with base64. Node.js provides global objects for accessing and importing modules....