Updated March 29, 2023
Introduction to Node.js global variable
Global variables are declared globally in the code and are accessible within the code anywhere. At the same time, node.js has some global objects, which are globally available. These objects are for help with coding. Some functionality is already written in node js globally. So we can directly access it via these global variables. These objects help the programmer to write the required code in less time. Global variables or objects are nothing but the repetitive must be task required to the programmer. Global itself suggests that this is in-built in the language and can be used by anyone. In this topic, we are going to learn about Node.js Global Variable.
How Global Variable Work in Node.js?
Node js has global objects to work with, but at the file level. And the file is nothing but the separate module In node js.
We know that for getting the output of any variable or object or for a debugging purpose, we use the console.log() statement mostly. Or the function like setTimeOut(). So how can we use this statement in any nodejs program without any problem? Have you ever thought about this? If don’t worry. This works as a global object. And this is the way it helps us. We also have many other objects and functions which are available globally to use in nodejs.
One thing to note here, if we are declaring any variable in nodejs, then it is not added to its global scope. So how can we achieve this? In node js, we have a file system. Every variable or function is scoped to that file only. Every file we are creating here with node js is called a module in node js. And whatever stuff we have written in that module is scoped to that respective module only.
How can we use the var or function globally in node js?
The following are some global objects we have in node js. Of course, there are many more, but the most important and widely used ones are shown below. We will also see examples of some. Now let’s look at the particular definition, what it does.
- Process: Process is a module in Nodejs that gives you the details about the node version and current node process. As this is global, it is available without requiring functions in the node js application. The process is an instance of EventEmitter.
If you type process and hit enter on node js command prompt, then you will get a lot more info about this object.
- require() : This function we use mostly in node js.This is used to import modules in the current module. Modules can be imported from node modules.
- Module: We can say that it works exactly opposite to require. This object is used to exporting the module. We can use module.export to export any module. Module.exports can be used by exports also. This is local for each module.
- exports: This is a reference for module.exports. exports is a shorter way of using module.exports. We must know when to use what. This is also a file-level scope.
- __dirname: Will see the example of this. But for now, just put in mind that we can get a directory of the file. For dynamic use of envirom specific file location, this is a very useful module.
- __filename: same as __dirname module, we use to get file name also along with directory.
Examples of Node.js Global Variable
Different examples are mentioned below:
Suppose we have a file and want to know in which directory this file is located, then this __dirname global variable in node js will help us.
Open a file and write the below code in it. And save it with a proper name with .js extension. We are saving it with global1.js
Run the file by the below command
After that, Run the below command
Now, if we want to know the file name, we have __filename as a global variable. This will show the file name along with the path.
We are going to write code in the same above file, which is global1.js. And we will follow the same process to run it.
If you observed the output carefully, we have got filename also, i.e., global1.
Now, if you see the output closely, you will notice that the file name also got printed. So, try to replicate the same code run it.
Suppose we want to use the filesystem module in our current application. Then you can import it with the required method, as shown in the below example.
const fs = require("FileSystem");
We can import local files also with this statement.
const fileOne = require('./path/myfolder/fileOne')
- The main benefit is reusability
- When other modules need some functionality, we can easily export it and then import it into our model.
- Ease of programming makes a smoother way of handling different requirements.
- Reduces the overload on programmers by using ready-to-use functionality.
- Gives an easier way of using a dynamic approach in different environments.
- Provides the flexibility to write programs.
Every language has its own way of declaring and using variables, nde js so. It is really an advantage but not every time. Node js has a module-based approach to work with global objects. It is way more helpful in nodejs. Once you understand the way they work, it will be an advantage for beginners on node js.
This is a guide to Node.js Global Variable. Here we discuss ,How we can use the var or function globally in node js and Examples along with the outputs. You may also have a look at the following articles to learn more –