Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js Global Variable

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 to 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.

If you are familiar with javascript, then you must know that we have a window object. This is the global object, and some variables and functions are globally defined under this. So, for example, we have seen a console.log() statement. But in node, we have a different scenario. Node is a server-side JavaScript run environment. That means the window object is browser-specific. So it will not help with node js. So in node js, we have one object called global. So whatever function we are using in nodejs without creating them is coming from this global object.

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 imp and widely used ones are shown below. We will also see examples of some of them. 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: We 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:

Example #1

__dirname:

Suppose we have a file and wanted 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

Console.log(__dirname);

Run the file by the below command

Command:

node global.js

After that, Run the below command

Command:

node global1

Output:

Example #2

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.

Syntax:

console.log( __filename);

Command:

node global1

Output:

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.

Example #3

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.

Example:

const fileOne = require(‘./path/myfolder/fileOne’)

Advantages

  • 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.
  • It reduces the overload on programmers by using ready-to-use functionality.
  • It gives an easier way of using a dynamic approach in different environments.
  • It provides the flexibility to write programs.

Conclusion

Every language has its own way of declaring and using variables, nde js so. It is really an advantage but not all the 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.

Recommended Articles

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 –

  1. Node.js Buffers
  2. Node.js File System
  3. Node.js Path
  4. Nodejs Architecture

The post Node.js Global Variable appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

Node.js Global Variable

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×