Global Variables in NodeJS

 

       Global variables are a set of variables which are available in every file executed through NodeJS without having to import any modules. These global variables perform some important functions which are very useful in various cases. Lets see the use of global variable with the help of a program.

Hello World Application 2.0

From our previous article, we have our server.js file as:

Now suppose we need to use any arguments that the user supplies in the node terminal or suppose that we need to use some environment variables (some variables specific to the current machine) to run our code accordingly. 

Let us change the code to say hello to a particular person.
Here we read the process.argv global variable to access the command line arguments passed to the script. process.argv is an array which consists of

  • The location to node executable file
  • The complete path of javascript you are executing
  • Command line arguments

Here is the output:

The speciality of the process global variable is that it can be accessed from anywhere without having to import any other module. There are many other global variables in NodeJS.  require is another global variable.

Let us have a look at some more useful global variables.

1. module 
   Module variable in a current file refers to the module which will be exported when this this file is required somewhere else. Objects can be made accessible to outside files by using module.exports.

See the screenshot. When we declare module.exports to be equal to some function, we are saying that whenever a file requires this module, export that function.

In the above code, we require the my-module module which we just created. When we call the function wish, it actually calls the function from the my-module.js file. See the output

Whenever you want a function to be exported by a module, you need to use module.exports

2. exports
exports is a short-hand to the module.exports object. exports always returns a json object exports can be used to export multiple objects (or functions) along with module.exports.
   


In this example, we use two functions defined in my-modules.js in our server.js file. We call these functions by their exported name.

3. __dirname
This variable stores the directory path of the file in which the currently executing function is defined.

4. __filename
This variable is similar to __dirname but instead of directory, it stores the absolute path of the file which is executing the current function. This is useful if you need to get the current filename or path.

5. global
      global is the global object which contains other global variables and functions. You can use global object to define new global variables as per your requirements. You can declare your own global variables using

global.<variable_name> = <value>                                                                                                      

Eg:
global.username = jibinmathews

This variable will be available in all the files which are run in the current process.

6. require
     require is used to import some modules into another modules. The usage of require is as follows:

  • require(“moduleName”) - This command searches for the moduleName module in the node_modules/ directory.
  • require(“./moduleName”) - This looks for a module named moduleName in the same directory from where the file requiring this module is located.
  • require(“./myFolder”) -  This would by default translate to  require(“./myFolder/index”)

7. process
     Process is a collection of global variables which define and interact with the current instance of the application. There are too many objects of process variables to be covered here. Some of the objects are:

  • process.argv - This is an array which contains the arguments which are passed through the command line while calling node.
  • process.cwd() - This is a function which returns the location from where the node script is invoked.
  • process.env - It is a JSON object containing all the environment variables declared in your system.
  • process.stdout , process.stdin, process.stderr -  These objects contains reference to standard output, standard input and standard error. 
  • process.exit(errorCode)  - Used to terminate the current node process.

No comments:
Write comments

Popular Posts