Most people advise against using global variables. If you want the same logger class in different modules you can do this

logger.js

  module.exports = new logger(customConfig);

foobar.js

  var logger = require('./logger');
  logger('barfoo');

If you do want a global variable you can do:

global.logger = new logger(customConfig);
Answer from Pickels on Stack Overflow
๐ŸŒ
Node.js
nodejs.org โ€บ api โ€บ globals.html
Global objects | Node.js v25.8.1 Documentation
In browsers, the top-level scope has traditionally been the global scope. This means that var something will define a new global variable, except within ECMAScript modules. In Node.js, this is different.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ node-js-global-objects
NodeJS Global Objects - GeeksforGeeks
January 17, 2026 - NodeJS HTTP2 ยท Last Updated : 17 Jan, 2026 ยท Node.js provides a global scope mechanism that allows certain variables and functions to be available across the entire application without explicit imports.
๐ŸŒ
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
1 week ago - It returns the name the of current working directory where the NodeJS process is being served from. It depends on the invoking node command. It is the nodeโ€™s global object. __dirname: It returns the directory name of the directory containing ...
๐ŸŒ
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 of what a global variable in Node is, let's talk about how we actually set up and use a global variable. To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather ...
Find elsewhere
๐ŸŒ
Node.js
nodejs.org โ€บ download โ€บ release โ€บ v14.16.1 โ€บ docs โ€บ api โ€บ globals.html
Global objects | Node.js v14.16.1 Documentation
In browsers, the top-level scope is the global scope. This means that within the browser var something will define a new global variable. In Node.js this is different.
๐ŸŒ
DEV Community
dev.to โ€บ rahulvijayvergiya โ€บ nodejs-vs-browser-understanding-the-global-scope-battle-39al
Node.js vs. Browser: Understanding the Global Scope Battle - DEV Community
July 15, 2024 - The Node.js global object is global, analogous to the window object in browsers. However, unlike the browser, variables declared with var, let, or const at the top level of a Node.js module do not become properties of the global object:
๐ŸŒ
Quora
quora.com โ€บ How-do-I-declare-a-global-variable-in-Node
How to declare a global variable in Node - Quora
Answer (1 of 2): Generally you don't want to. If you think you want to, then rethink your plan and don't do it. For instance, if you need a value shared between modules, your can export it from one and import it where it's needed. But the way to actually add something to the global namespace i...
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_scope.asp
W3Schools.com
Variables declared Globally (outside any block or function) have Global Scope.
๐ŸŒ
Plain English
plainenglish.io โ€บ blog โ€บ how-to-create-global-variables-in-typescript-with-node-js-9ca24f648991
How to Create Global Variables in TypeScript with Node.js
December 13, 2021 - However, I found an approach you may find useful, letโ€™s suppose you want to use globalString in some file. First, you have to initialize your global variable, I suggest you to do it in the entry point of your app (just to be organized), it will look something like:
๐ŸŒ
Medium
habtesoft.medium.com โ€บ global-variables-in-javascript-understanding-and-best-practices-dbd7dd6f1067
Global Variables in JavaScript: Understanding and Best Practices | by habtesoft | Medium
November 13, 2024 - Global Variables in JavaScript: Understanding and Best Practices In JavaScript, global variables are variables that are accessible from any part of your code, regardless of where they are declared โ€ฆ
๐ŸŒ
Reddit
reddit.com โ€บ r/node โ€บ how to deal with global variables when modularizing a node.js app?
r/node on Reddit: How to deal with global variables when modularizing a node.js app?
May 7, 2016 -

Hey all,

I have been working on an api for the last 2 months and my main index.js page has reached 2000 lines of code. I now need to modularize my application into submodules.

My main problem is I don't know how I will implement global variables into a modularized structure. For objects and arrays, I can just pass them down to my function has an argument since they are passed by reference but for every other datatype, I can't pass them as a reference.

What is the most common way to deal with modularizing a node.js app which has global variables?

EDIT: Specifically, how would you deal with global variables in Koa.js?

Top answer
1 of 2
3
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.
2 of 2
2
Generally speaking you should try to avoid globals, preferring to expose state only to those parts of a program that need it. One step towards this is using modules. For a simple fix you can just move all your global state into its own module, and require it wherever you need it. The value exported by a module is shared between any other modules that export it, so it's possible to share state that way. For example: // index.js var global_a = 5; var global_b = 6; function uses_a() { return global_a * 2; } function uses_b() { return global_b * 2; } console.log('' + uses_a() + uses_b()); becomes: index.js var uses_a = require('./uses_a'); var uses_b = require('./uses_b'); console.log('' + uses_a() + uses_b()); state.js module.exports = { global_a: 5, global_b: 6 }; uses_a.js var state = require('./state'): module.exports = function uses_a() { return state.global_a * 2; } uses_b.js var state = require('./state'); module.exports = function uses_b() { return global_b * 2; } There is also a global object global that can be used to share state between modules. Its use is discouraged though, as it is shared between all modules (including those from your node_modules directory). // accessible anywhere: global.global_a = 5;
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ nodejs โ€บ nodejs_global_objects.htm
Node.js - Global Objects
Process ID: 40096 Node.js Version: ... 7614464, heapUsed: 6585976, external: 2339203, arrayBuffers: 395682 } The __dirname and __filename are the two important global variables in Node.js that provides the information about ...
๐ŸŒ
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 - The global object is the root object in a Node.js application, acting as the global context for the runtime environment. It allows variables or functions to be globally accessible, similar to the window object in client-side JavaScript.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ how-to-create-global-variables-accessible-in-all-views-using-express-nodejs
How to Create Global Variables Accessible in all Views using Express/NodeJS ? - GeeksforGeeks
April 9, 2024 - By assigning a value to res.locals.variableName, you create a local variable that is available only within the current request-response cycle. This approach is useful for passing dynamic data to views based on each request, such as user authentication status or request-specific information. Example: Illustration to create global variable using res.locals
๐ŸŒ
Adamtuttle
adamtuttle.codes โ€บ blog โ€บ 2022 โ€บ aws-lambda-safe-global-variables
Global node.js Variables Are Safe to Use in AWS Lambda โ€ข Adam Tuttle
July 26, 2022 - Given the multiple-request model of Express[1], if you write a global variable during a request, and then later reference that same global variable during the same request, there is no guarantee that it will contain the same value that you previously wrote.
๐ŸŒ
Greenroots
blog.greenroots.info โ€บ nodejs-knowing-the-global-objects
Node.js - Knowing the Global Objects
July 19, 2021 - 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 Node.js module, it will be local to this module.
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Grammar_and_types
Grammar and types - JavaScript | MDN
... Unlike var declarations, which only hoist the declaration but not its value, function declarations are hoisted entirely โ€” you can safely call the function anywhere in its scope. See the hoisting glossary entry for more discussion. Global variables are in fact properties of the global object.