How to create global variables in Node.js#

So you are working with a set of Node modules, maybe a framework like Express.js, and suddenly feel the need to make some variables global. How do you make variables global in Node.js?

The most common advice to this one is to either "declare the variable without the var keyword" or "add the variable to the global object" or "add the variable to the GLOBAL object". Which one do you use?

First off, let's analyze the global object. Open a terminal, start a Node REPL (prompt).

$ node

Type global at the prompt and hit enter to see what the object is all about:

> global

Holy mother of Flying Spaghetti Monster!!! That's one huge object! Infact, you saw the soul of Node. All other objects in a Node process piggyback on this object. If you are familiar with the JavaScript environment in a browser, the global object is equivalent to the window object.

Now that we know what the global object is, let's play around with it:

> = 'El Capitan'
'El Capitan'
'El Capitan'
> delete
> name = 'El Capitan'
'El Capitan'
'El Capitan'
'El Capitan'
> var name = 'Sparrow'

Interesting observations!

Looks like global and GLOBAL are one and the same thing. Indeed, GLOBAL is an alias for global.

Even more surprising was that a variable declared with or without the var keyword got attached to the global object. This is the basis for creating global variables in Node by declaring variables without the var keyword. This works slightly different in a module, I will explain it next.

When you start a Node process, the module which started it, and all the modules included in it, all share the same global object. Apply the above observations with this fact and you have a good idea of how global variables work in Node. However, there is a slight variation - variables declared with the var keyword remain local to a module; those declared without it get attached to the global object.

So now you know, "declare the variable without the var keyword", "add the variable to the global object", and "add the variable to the GLOBAL object", all are the same thing.

Globally declared variables in a module can be referenced from any module with just their name, without having to refer them from the global object - name == But that does not mean you should do that. Why? Look at this:

var company = 'Yahoo';
console.log(; // 'Google'
console.log(company); // 'Yahoo'

When you use, you know you are dealing with a global variable, besides it spares the name company for use as a local variable within the module.

If you intend to use global variables in your Node application, the discussed global object method works fine. Try not to overuse it, though. Having said that, could there be an alternative solution which doesn't use the global object?

Yes there is one, and it involves the use of module.exports. Let me demonstrate using an example:

main.js = 'Google';
var m = require('./mod');
var company = require('./main').company;

Now to see it in action:

$ node main.js

There you have it - a variable from one module made available in another without using the global object. You can include main.js in other modules to get access to the company name.

Including a module which was already included in another module just creates a reference to the previous inclusion, so it does not mean a drastic increase in RAM. Also, since the there is no real re-inclusion, all the init functions in the module are not executed again.

So, in conclusion, there are two ways of creating global variables in Node.js, one uses the global object, and the other uses module.exports. What is my recommendation? global method for small apps, module.exports for big apps.


  1. Node.js Global Objects
  2. global