global nodejs keyword and object

Mohamed Lamine Allal
4 min readMar 19, 2021

We all may know about global and scope nature and all! But this property may slipped for you (How to define globals)

Here i want to highlight one property of global !

What you put there is accessible also directly (that define globals)

(make sure to check the Property title and section)

Before bringing the property! Let’s define global again!

global is a language keyword specific to nodejs and reference The global namespace object

As it was already described on other answers! The top scope in a module! Is not global! And limited to only that module!

So when you declare a variable in one module you can’t access it in another!

https://nodejs.org/api/globals.html#globals_global

The global namespace is accessible everywhere in a given process! In all modules! That’s include your own module and third party modules!

console Logging global in node repl will give this:

Welcome to Node.js v13.14.0.
Type ".help" for more information.
> console.log(global)
<ref *1> Object [global] {
global: [Circular *1],
clearInterval: [Function: clearInterval],
clearTimeout: [Function: clearTimeout],
setInterval: [Function: setInterval],
setTimeout: [Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Function (anonymous)]
},
queueMicrotask: [Function: queueMicrotask],
clearImmediate: [Function: clearImmediate],
setImmediate: [Function: setImmediate] {
[Symbol(nodejs.util.promisify.custom)]: [Function (anonymous)]
}
}
undefined

The property: What you put there is accessible also directly

What i wanted to bring is this! I get to notice this when i was exploring laravel-mix code source!

If you set something on the global object! as like global.Hola = { print: () => console.log("Hola") };. Then you can access the variable by it's name directly any where in the project code (multiple files [modules] & Whole node process code)! Meaning Hola.print() in place of global.Hola.print()!

You can call that defining globals! As my friend Motiaa Benachour named it! When i shown it to him (By the way he is a pretty cool developer and friend)!

Here a node repl screenshot for the example above:

> global.Hola = { print: () => console.log('Hola') }
{ print: [Function: print] }
> Hola.print()
Hola
undefined

Nice property ! That’s the global namespace!

You can notice the methods like clearInteraval, clearTimeout, setInterval, setTimeout, ... are defined there! And we used to access them directly by there name!

Laravel mix example

Here some examples from laravel-mix code source! Where it use that!

If you open this file: https://github.com/JeffreyWay/laravel-mix/blob/master/src/components/ComponentRegistrar.js

You notice in the import section there is no! Mix variable neither Config! But they are used and part of the code! I was like: what the heck!

Imort code:

let Assert = require('../Assert');
let Dependencies = require('../Dependencies');
let mergeWebpackConfig = require('../builder/MergeWebpackConfig');

On line 68 : (link here) you can see the usage of Mix class variable!

And same thing for Config at line 178 (link here)

When i first got to see it! And get to check the import section! And using github reference feature (came with nothing)! I was What the heck!

Later on when i checked the Mix.js file! And class! I found the code that set them! I got the intuition and googled after!

Conflicts and Why one would want to use the global namespace

A problem to setting globals is overriding and conflicts! Which leads to bugs and unexpected behaviors, up to a total crash! If modules start to use it without thoughts! One module will screw it for another! Like when using a third party module! Imagine module request will set Config var! And you set it too! Or even another third party module! And they are all dependent on it! One will screw it for the other module!

So simply put! We should not use globals! No no! And yes!

It all depends!

It’s preferable for a module to not do! That way the module is completly isolated! And it’s more robust! Generally setting the variable in a certain module! And importing it each time! Using dependency injection …etc

In many cases however it’s more flexible to use the global namespace!

You can do so and not worry! If you are building a server! Config object can go on the global! A command line Interface tool or script! Some process that run directly!

Generally don’t use the global scope when you are building a module! A package! A library! A component! Which gonna be reusable! (Reusable cross projects! no global scope! isolate it)!

Laravel mix for instance is a package that is used to generate webpack config! That get to run as a cli tool and process!

However if the Config variable for example was also set by Webpack or some of the community plugins or loaders! Then problems can occure due to overriding!

Something simple could make it safer is to add a domain in the naming! For instance Mix_Config !

--

--

Mohamed Lamine Allal

Developper, Entrepreneur, CTO, Writer! A magic chaser! And A passionate thinker! Obsessed with Performance! And magic for life! And deep thinking!