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 >
global at the prompt and hit enter to see what the object is all about:
global object is equivalent to the
Now that we know what the global object is, let's play around with it:
> global.name undefined > global.name = 'El Capitan' > global.name 'El Capitan' > GLOBAL.name 'El Capitan' > delete global.name true > GLOBAL.name undefined > name = 'El Capitan' 'El Capitan' > global.name 'El Capitan' > GLOBAL.name 'El Capitan' > var name = 'Sparrow' undefined > global.name 'Sparrow'
GLOBAL are one and the same thing. Indeed,
GLOBAL is an alias for
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
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 -
global.name. But that does not mean you should do that. Why? Look at this:
var company = 'Yahoo'; console.log(global.company); // 'Google' console.log(company); // 'Yahoo'
When you use
global.company, 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
Yes there is one, and it involves the use of
module.exports. Let me demonstrate using an example:
exports.company = 'Google'; var m = require('./mod');
var company = require('./main').company; console.log(company);
Now to see it in action:
$ node main.js Google
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.
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.