Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What are Global Objects in Node JS?

Updated on 12 June, 2023

8.36K+ views
7 min read

Introduction

Global object in Node.js offers crucial functionality and is the basis for constructing Node.js applications. These objects offer various features and conveniences, providing developers access to file systems, event handling, module administration, network activity execution, and more. Designing productive and resilient Node.js apps requires awareness of and good usage of these global objects. Follow this blog for more insights.

upGrad offers a Full Stack Software Development Bootcamp programme designed by industry experts to teach job-ready software skills. Learn to build a web application’s backend using Node.js global variable and ExpressJS, perform CRUD operations with MongoDB and develop a dynamic, real-time frontend with React.

What Is Node.js?

Node.js is a JavaScript runtime that enables developers to write client-side and server-side programmes in JavaScript without learning another language. It is neither a programming language nor a software development framework. Thanks to this cross-platform and open-source JavaScript runtime environment, developers can run JavaScript code outside a web browser. It uses the V8 JavaScript engine that powers Google Chrome and other browsers. 

Because of its practical and scalable design, Node.js is well-suited for developing fast and scalable server-side and networking applications. It uses an event-driven, non-blocking I/O (input/output) paradigm to handle multiple requests simultaneously without compromising future processing.

Here’s a code snippet that provides a basic example of how to define a Node.js server using the Express framework:

const express = require('express');

const app = express();

const port = 3000;

app.get('/', (req, res) => {

  res.send('Hello, World!');

});

app.listen(port, () => {

  console.log(`Server listening at http://localhost:${port}`);

});

Depending on the needs, one can create different codes to handle more routes, interact with databases, and carry out various other tasks in Node.js.

Check Out upGrad’s Software Development Courses to upskill yourself.

What Are Global Objects in Node.js?

The global object in Node.js is called ‘global‘. It provides access to several built-in objects, including ‘process’, ‘console’, ‘buffer’, ‘setImmediate()’, ‘clearImmediate()’, and ‘setTimeout()‘, etc. For instance, the process object, an instance of EventEmitter, can be accessed from anywhere in the application, providing details about the running Node.js process. The console object prints to the standard output and standard error streams, while the buffer object handles data in binary format.

In contrast to Node.js, global variables specified in a web browser using the var keyword are not formed as members of the global object.

Why Are Global Objects Used in Node.js?

Global objects in Node.js ease the retrieval of system-level data, the transfer of functionality between modules, and data access across modules. Some of the common uses are:

  • Provides a technique for transferring objects between modules while keeping data and functionality intact.
  • It can be used to declare constants or configuration parameters the application employs.
  • Used to identify critical features that the application will always utilise.
  • It can be used to acquire system-level information such as operating status or environmental conditions.
  • Used to design event emitters that broadcast and receive events across modules.
  • Used to create timers to plan future code execution.

Commonly Used Global Objects in Node.js

Global objects in Node.js are pre-built objects that can be used in any module without including them in the application. These items are modules, functions, strings, and actual objects. While some items are accessible from anywhere, others are at the module’s level.

Here are some commonly used global objects in Node.js:

The “console” object

In Node.js, the console object writes data to the standard error (stderr) and standard output (stdout) streams. Console.log(), console.error(), console.warn(), and console.info() are among the methods in this library that display different forms of information. The console object is implemented at a lower level using the process.stdout.write() method.

Here’s an example of using the console object in Node.js:

console.log('Hello, world!');

console.error('This is an error message.');

console.warn('This is a warning message.');

console.info('This is an informational message.');

This code uses the console object to print different types of messages to the console. The output of this code would be:

Hello, world!

This is an error message.

This is a warning message.

This is an informational message.

The “process” object

The process object in Node.js is a global object easily accessible from anywhere. It is an essential component of the Node.js ecosystem since it provides various data sets relevant to an application’s operation. 

The process object, an instance of the EventEmitter class, includes built-in events like exit that can be used to determine when a Node.js application has finished executing. Furthermore, the process object has various other operational properties. Some of these can be incorporated into a Node.js application as a conduit between a command-line interface and the Node.js program.

Here’s an example of using the process object to get the current working directory:

console.log(`Current directory: ${process.cwd()}`);

This program uses the process.cwd() method to get the current working directory and prints it to the console.

Another example is obtaining the command-line arguments supplied to a Node.js program using the process.argv property:

console.log(`Command-line arguments: ${process.argv}`);

This program uses the process.argv property to get the command-line arguments passed to the Node.js program and prints them to the console.

Other attributes of the process object, including env, pid, title, uptime, and memoryUsage, can be used to obtain details about the running process.

The “buffer” object

The Buffer object in Node.js directly manipulates binary data and can be created in various ways. Binary data is represented as a collection of bytes via the Buffer class. Creating a Buffer class instance can convert text into a binary data stream. An application can access the Buffer class without importing the buffer module.

Here’s an example of how to create a Buffer object in Node.js:

// Create an uninitiated Buffer of 10 octets

const buf1 = Buffer.alloc(10);

// Create a Buffer from a given array

const buf2 = Buffer.from([10, 20, 30, 40, 50]);

// Create a Buffer from a given string and optionally encoding type

const buf3 = Buffer.from('Simply Easy Learning', 'utf-8');

The “setInterval” and “clearInterval” methods

In Node.js, the setInterval() method continually executes a function after a fixed delay. It produces a unique interval ID which the clearInterval can subsequently use to halt the function’s repeated future execution.

Here’s an example of using setInterval() and clearInterval() in Node.js:

function sayHello() {

  console.log('Hello!');

}

// Call sayHello() every 1 second

const intervalId = setInterval(sayHello, 1000);

// Stop calling sayHello() after 5 seconds

setTimeout(() => {

  clearInterval(intervalId);

}, 5000);

In this example, the sayHello() method is executed every 1 second using setInterval(). After 5 seconds, clearInterval() is called with the interval ID given by setInterval() to terminate the continued execution of the method.

The clearInterval() function accepts the interval ID returned by setInterval() as its input. If the argument supplied does not indicate an already existing action, this function performs nothing.

upGrad offers a Master of Science in Computer Science from LJMU (Liverpool John Moores University) that specialises in full stack development, focusing on Node.js. 

Conclusion

The global objects by Node.js provide numerous capabilities and utilities, allowing developers to interface with the file system, handle events, manage modules, execute network activities, and more. Enrolling in a suitable course can help master real-time insights in Node.js. 

Check out the Executive PG Programme in Full Stack Development from IIITB, a 13-month online programme co-developed by upGrad. The programme offers the right blend of statistics, technical, and business knowledge. Check out the website for further details.

Frequently Asked Questions (FAQs)

1. How to declare a global variable in Node.js?

One can use the global keyword to declare a global variable in a JavaScript runtime environment, Node.js. To create a global variable counter, for instance, one can use the code below: global.counter = 0; However, using global variables is usually discouraged to avoid naming conflicts.

2. What is the difference between global and default scope in Node.js application?

Default scope in Node.js refers to variables declared inside a function that can only be used within the function that declares them. Global scope in Node.js refers to variables available from any program component. Using default variables is usually recommended instead of global variables since it might lead to name disputes and make the code harder to maintain.

3. What is a JavaScript global variable?

A global variable in JavaScript is defined outside any function and accessible from any function. Global variables are global object properties, which in the case of web browsers is the window object. A global variable in Node.js can be accessed from anywhere in the application, regardless of the file or function scope.

RELATED PROGRAMS