Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js Process exit Event
Next article icon

Global, Process and buffer in Node.js

Last Updated : 05 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Global: Global objects in node.js is available in all the modules and are scoped locally to their respective modules.

Some of the global objects are:

  • exports
  • module
  • require
  • __filename
  • __dirname

The above objects and require functions can be accessed everywhere without importing.

Process: A process object is a global object that gives information about and controls the node.js process. As it is global, it can be used in the project without importing it from any module. 

It is an instance of EventEmitter class and has many useful methods that help us in knowing more about the processes that happened and also about currently undergoing the process.

Important events of the process:

beforeExit Event: This event is triggered when the Node.js event loop is getting hollow and has no additional work scheduled. In general cases, the Node.js process will exit when there is no process left, but a listener registered on the ‘beforeExit’ can make asynchronous calls.

Javascript




const process = require('process');
 
process.on('beforeExit', (data) => {
console.log('Process beforeExit event with code.');
});
 
process.on('exit', (data) => {
console.log('Process exit event with code');
});
 
console.log('This code is rendered first.');
 
 

Output: 

This code is rendered first. Process beforeExit event with code. Process exit event with code.

Exit Event: The ‘exit’ event is emitted when the Node.js process is about to exit as a result of either:

  • The process.exit() method is called explicitly.
  • The Node.js event loop no longer has any additional work to perform.

There is no way to prevent the exiting of the event loop at this point, and once all ‘exit’ listeners have finished running the Node.js process will terminate.

Javascript




process.on('exit', (data) => {
    console.log(`code execution is going to end `);
});
 
 

Output: 

code execution is going to end

Apart from the above two main events, there are also many more events that come with process objects. 

Buffer: The Buffer class in Node.js is made to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren’t resizable and have a whole bunch of methods specifically for binary data.

Creating a buffer: 

let buffer = Buffer.alloc(6); 

Output:

This will print out 6 bytes of zero
let buffer = Buffer.from("Welcome to GeeksforGeeks!", "utf-8");

Output:

This will print out a chain of values in utf-8

Writing to a buffer: If it contains two arguments, the first argument is the data and the second argument is the type of encoding.

buffer.write("GeeksForGeeks", "utf-8")

Output:

This will print out 13 as size of buffer

Reading a buffer: We can use the toString() method to read a buffer.

Javascript




let buf = Buffer.from('GeeksForGeeks');
console.log(buf.toString());
 
 

Output:

GeeksForGeeks


Next Article
Node.js Process exit Event

B

bunnyram19
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • Node.js-Misc

Similar Reads

  • Difference Between process.cwd() and __dirname in Node.js
    In Node.js, both process.cwd() and __dirname are used to get the directory paths, but they serve different purposes. The key difference is that process.cwd() returns the current working directory from which the Node.js process was started, while __dirname returns the directory name of the current mo
    3 min read
  • How to Exit Process in Node.js ?
    In this article, we will see how to exit in NodeJS application. There are different types of methods to exit in Nodejs application, here we have discussed the following four methods. Table of Content Using ctrl+C keyUsing process.exit() FunctionUsing process.exitCode variableUsing process.on() Funct
    3 min read
  • Node.js Process exit Event
    The process is the global object in Node.js that keeps track of and contains all the information of the particular node.js process that is executing at a particular time on the machine.  The process.exit() method is the method that is used to end the Node.js process. Every process action on the mach
    2 min read
  • Node.js Buffer.buffer Property
    The Buffer.buffer property is an inbuilt application programming interface of class Buffer within buffer module which is used to get the object of array buffer equivalent to this buffer object. Syntax: const buf.buffer Return Value: This property returns the object of array buffer. Example 1: Filena
    1 min read
  • What are Buffers in Node.js ?
    Buffers are an essential concept in Node.js, especially when working with binary data streams such as files, network protocols, or image processing. Unlike JavaScript, which is typically used to handle text-based data, Node.js provides buffers to manage raw binary data. This article delves into what
    4 min read
  • Node.js Process beforeExit Event
    The 'beforeExit' is an event of class Process within the process module which is emitted when Node.js empties its event loop and has no additional work to schedule. Syntax: Event: 'beforeExit'Parameters: This event does not accept any argument as the parameter. Return Value: This event returns nothi
    2 min read
  • How to read and write files in Node JS ?
    NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
    2 min read
  • Node.js Buffer Complete Reference
    Buffers are instances of the Buffer class in Node.js. Buffers are designed to handle binary raw data. Buffers allocate raw memory outside the V8 heap. Buffer class is a global class so it can be used without importing the Buffer module in an application. Example: C/C++ Code <script> // Node.js
    8 min read
  • Explain the Process Object in Node.js
    In this article, we are going to explore about process object of Node.js in detail. The Process object in node.js is a global object that can be used in any module without requiring it in the environment. This object is useful in the perspective of getting the information related to the node.js envi
    3 min read
  • Node.js Buffer.byteOffset Property
    The Buffer.byteOffset property is an inbuilt application programming interface of class Buffer within buffer module which is used to get the byte offset value of this buffer. Syntax: const Buffer.byteOffset Return Value: This property return the object of array buffer. Example 1: Filename: index.js
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences