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 Signal Events
Next article icon

Node.js Process exit Event

Last Updated : 30 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 machine or a program is an event. For every event, there is even a handler associated with the particular event that executes when we fire the particular event. To assign an event handler to the event we use the object.on() method in the node.js. In this article, we will discuss process exit event in Node.js

Syntax:

process.on("exit", callbackfunction)

Parameters: This method takes the following two parameters.

  • exit: It is the name of the emit event in the process.
  • callbackfunction: It is the event handler of the event.

Return Type: The return type of this method is void.

Example 1:

index.js
console.log("Starting of the process")  // Binding the event to the eventhandler process.on('exit',() => {     console.log("process.exit() method is fired") })  console.log("Ending of the process")  // Exiting the process process.exit() 

Run index.js file using below command:

node index.js

Output:

Starting of the process  Ending of the process  process.exit() method is fired

Example 2: Creating process exit event handler inside the user-defined event handler.

index.js
// Importing events object const events = require("events")  console.log("Starting of the process") const eventEmitter = new events.EventEmitter()  // Initializing ing event Handler var Handler = function() {     // Event handler of exit event    process.on('exit', () => {       console.log("process.exit() method is fired")    }) }  // Bind the  user defined event  eventEmitter.on("hello",Handler)  // Emit the event eventEmitter.emit("hello")  console.log("Ending of the process")  // Exiting the process process.exit() 

Run index.js file using below command:

node index.js

Output:

Starting of the process  Ending of the process  process.exit() method is fired

Reference:https://nodejs.org/api/process.html#process_event_exit


Next Article
Node.js Process Signal Events
author
zack_aayush
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-process-module

Similar Reads

  • 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
  • Node.js Process Signal Events
    Signals are a POSIX (The Portable Operating System Interface) intercommunication system. These events will be emitted when Node receives signal events. In order to notify that an event has occurred, a notification is sent. The signal handler will receive the signal's name and the signal name is uppe
    3 min read
  • Node.js Process message Event
    The 'message' is an event of class Process within the process module which is emitted whenever a message sent by a parent process using childprocess.send() is received by the child process. Syntax: Event: 'message'Parameters: This event does not accept any argument as a parameter. Return Value: This
    2 min read
  • Node.js Process warning Event
    The 'warning' is an event of class Process within the process module which is emitted whenever Node.js emits a process warning. Syntax: Event: 'warning'Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function for further ope
    2 min read
  • Node.js process.exit() Method
    The process.exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code ) Parameter:  This function accepts single parameter as mentioned above and described below: Code: It can be either 0 or 1. 0 means end the process without a
    2 min read
  • Node.js Process disconnect Event
    In this article, we will discuss about the disconnect event of the Process object. The disconnect event will be issued when the IPC channel is closed if the Node.js process was started using an IPC channel. Syntax: process.on('disconnect', () => { // Disconnect event }); The below examples illust
    2 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.geteuid() Method
    The process.geteuid() method is an inbuilt application programming interface of the process module which is used to get the numerical effective user identity of the Node.js process. Syntax: process.geteuid() Parameters: This method does not accept any parameters. Return Value: This method returns an
    2 min read
  • Node.js process.getegid() Method
    The process.getegid() method is an inbuilt application programming interface of the process module which is used to get the numerical effective group identity of the Node.js process. Syntax: process.getegid() Parameters: This method does not accept any parameters. Return Value: It returns an object
    2 min read
  • Node.js process.getgid() Method
    The process.getgid() method is an inbuilt application programming interface of the process module which is used to get the numerical group identity of the Node.js process. Syntax: process.getgid() Parameters: This method does not accept any parameters. Return Value: It returns an object specifying t
    1 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