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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_object
Global object - Glossary | MDN
The global object in JavaScript is an object which represents the global scope.
Top answer
1 of 7
102

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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
July 1, 2022 - The global object holds variables that should be available everywhere. That includes JavaScript built-ins, such as Array and environment-specific values, such as window.innerHeight – the window height in the browser.
🌐
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 - We’ll explain how to use globalThis to access the global object later in this article — but first, it’s important to understand why it was necessary. ... scopes (also called contexts) determine where a variable or function that you have declared is accessible. Each scope has access to the functions and variables declared in its parent scopes. There is a precedence to how JavaScript resolves variables that appear inside scopes: if variables of the same name have been declared in multiple scopes, JavaScript uses the first one it finds, starting at the current scope, and working up toward the global scope.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript global object
JavaScript Global Object
September 1, 2008 - The JavaScript global object allows you to access the variables, functions, objects, etc., defined in the global scope and available everywhere in the code.
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
With JavaScript, the global scope is the JavaScript environment. In HTML, the global scope is the window object.
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › node-js-global-objects
NodeJS Global Objects - GeeksforGeeks
January 17, 2026 - In Node.js, the global object provides a shared scope for variables and functions, similar to the window object in browsers. global in Node.js serves the same purpose as window in browsers.
🌐
Strapi
strapi.io › blog › global-object-in-javascript
What Is the Global Object in JavaScript?
April 30, 2025 - The global object in JavaScript forms the foundation of the language's runtime environment. It’s where all global variables, functions, and properties live, making it the top-level scope that any part of your code can access.
Top answer
1 of 2
1

Why

It's hard to say exactly why this feature exists, but it's useful in some situations. For example, if you're using Webpack or some other packer or ES modules, variables delcared with var will not be stored in the global object and thus will not be accessible through other files. As such, if you want a variable to be available everywhere, you need to declare it directly via the global object (globalThis.varName = value).

Global Scope vs Global Object

As I noted above, variables declared inside ES modules will not be stored in the global object. You can think of the properties on the global object as variables on a "super-global" scope, above the global scope.

While the global scope includes all variables decalerd with var, let or const outside any functions in a file, the global object may or may not include all variables decalerd with var outside any functions in a file.

What ends up in the global object, the scope of the global object and how many global objects there are depends on the environment.

2 of 2
0

I cannot read the mind of Brendan Eich, but I can take an educated guess as to why the global object exists:

First of all, the design of JavaScript is kept very simple: Records (the place where variable values get stored) and Objects are very similar. Both are basically a collection of key-value pairs. That simplifies the design (e.g. a = 1 works the same way as a.b = 1) and has the side effect that objects can easily be used as records (e.g. with the with statement).

Secondly you might also want to refer to global variables although the variable got shadowed. That can easily be achieved with having a self reference inside the global object / record (window is just a self reference).

Therefore it does make sense that var creates a variable in the global record, which, you can also refer to via window..

Actually that const and let do not create a variable on the global scope is the inconsistency, but with that modularity gets achieved.

🌐
Adobe
acrobatusers.com › tutorials › global-object-acrobat-javascript
The global object in Acrobat JavaScript
December 12, 2011 - The Acrobat JavaScript model has a global object for holding data that is global to the entire model. Below is a simple example of how it is used.
🌐
Cycling '74
docs.cycling74.com › max5 › vignettes › js › jsglobalobject.html
The Global Object
The Global object is a fairly generic Javascript object that allows you to share data among js instances by adding and accessing properties. You can also access Global object properties from Max messages completely outside of js. Executing methods stored in Global objects from Max is not supported.
🌐
Node.js
nodejs.org › api › globals.html
Global objects | Node.js v25.8.1 Documentation
Stability: 3 - Legacy. Use globalThis instead. Type: <Object> The global namespace object.
🌐
The Valley of Code
thevalleyofcode.com › javascript-global-object
The JavaScript Global Object
JavaScript provides a global object which has a set of properties, functions and objects that are accessed globally, without a namespace.
🌐
Medium
medium.com › @a.kago1988 › global-objects-variables-and-methods-in-javascript-and-node-js-f6bffc74e792
Objects, Variables, and Methods Built Into Node.js. What the Runtime Provides | by Andreas 🎧 Kagoshima | Medium
3 weeks ago - At the heart of JavaScript’s execution environments are built-in global objects — special objects architected to provide access to essential functions, variables, and methods across your application architecture.
🌐
Exploring JS
exploringjs.com › deep-js › ch_global-scope.html
A detailed look at global variables • Deep JavaScript
In this case, the object is the global object. A normal (declarative) environment record that has its own storage for its bindings. Which of these two records is used when will be explained soon. In JavaScript, we are only in global scope at the top levels of scripts.
🌐
W3Schools
w3schools.com › jsref › jsref_obj_global.asp
JavaScript Global Reference
Since these methods are global, and in a web browser the global object is the browser window, these methods are actually window methods: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
Quora
quora.com › In-Javascript-Window-is-the-global-object-It-is-well-known-but-what-does-this-actually-mean-The-term-“window”-refers-to-the-global-scope-object-and-each-global-can-be-accessed-with-“window-nameOfVar-”-Is-this-all-it-means
In Javascript, Window is the global object. It is well known, but what does this actually mean? The term “window” refers to the global sc...
Answer (1 of 5): You’re mostly correct, with a couple of caveats: 1. The window object is only the global scope in a browser. other javascript environments have a different global scope 2. Besides variables declared in the global scope, window is also the parent object for the rest of the browse...